ralate2 commited on
Commit
858b949
·
verified ·
1 Parent(s): a5192ab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -122
app.py CHANGED
@@ -435,129 +435,19 @@ elif viz_type == "Complaints by Housing Block and Type":
435
  The 'inferno' color palette is used to represent different complaint types, with darker shades indicating a higher frequency of complaints. The stacked bar chart makes it easy to compare the distribution of complaints by block and type.
436
  """)
437
 
438
- # elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestions Based on Professor's Feedback)":
439
- # st.subheader("Complaints by Housing Block and Type- Incorporating Suggestions Based on Professor's Feedback")
440
-
441
- # # Filtering the data based on the selected year and housing block
442
- # filtered_data_time = data # Use filtered_data if date range is not needed
443
- # if selected_year != 'All Time':
444
- # filtered_data_time = filtered_data_time[filtered_data_time['Year Reported'] == selected_year]
445
-
446
- # # Further filtering by Housing Block (if applicable)
447
- # if selected_block != 'All Blocks':
448
- # filtered_data_time = filtered_data_time[filtered_data_time['Housing Block'] == selected_block]
449
-
450
- # # Pivoting the data based on the filtered data
451
- # complaint_pivot = filtered_data_time.pivot_table(
452
- # index='Housing Block',
453
- # columns='Type of Complaint',
454
- # values='Disposition',
455
- # aggfunc='count',
456
- # fill_value=0
457
- # )
458
-
459
- # # Ensuring the pivoted data is numeric for plotting
460
- # complaint_pivot = complaint_pivot.astype(float)
461
-
462
- # # Desired order for the housing blocks
463
- # desired_order = [
464
- # '1 block', '100 block', '200 block', '300 block', '400 block', '500 block',
465
- # '600 block', '700 block', '800 block', '900 block', '1000 block', '1100 block',
466
- # '1200 block', '1300 block', '1400 block', '1500 block', '1600 block',
467
- # '1700 block', '1800 block', '1900 block', '2000 block', '2100 block',
468
- # '2200 block', '2300 block', '2400 block', '2500 block', '2600 block',
469
- # '2700 block', '2800 block', '2900 block', '3000 block', '3100 block',
470
- # '3200 block', '3300 block', '3400 block', '3500 block', '3600 block',
471
- # '3700 block', '3800 block', '3900 block', '4000 block', '4100 block',
472
- # '4200 block', '4300 block', '4400 block', '4500 block', '4600 block',
473
- # '4700 block', '4800 block', '4900 block', '5000 block'
474
- # ]
475
-
476
- # # Reordering the index of the pivot table according to the desired order
477
- # complaint_pivot = complaint_pivot.reindex(desired_order)
478
-
479
- # # Calculating percentages for each complaint type per housing block
480
- # percentages = complaint_pivot.div(complaint_pivot.sum(axis=1), axis=0) * 100
481
-
482
- # # Plotting the data
483
- # fig = complaint_pivot.plot(kind='bar', stacked=True, colormap='inferno', figsize=(10, 6)).get_figure()
484
-
485
- # # Adding percentage labels to the plot
486
- # ax = fig.gca()
487
- # for idx, block in enumerate(complaint_pivot.index):
488
- # cumulative_height = 0
489
- # for i, complaint_type in enumerate(complaint_pivot.columns):
490
- # count = complaint_pivot.iloc[idx, i]
491
- # percent = percentages.iloc[idx, i]
492
- # if count > 0:
493
- # # Compute the position for the percentage label
494
- # x_pos = idx - 0.4 + 0.8 / 2 # Adjusting the position of the label
495
- # y_pos = cumulative_height + count / 2
496
- # ax.text(
497
- # x_pos, y_pos, f"{percent:.1f}%",
498
- # ha='center', va='center',
499
- # fontsize=10, color='black',
500
- # bbox=dict(facecolor='white', alpha=0.7, edgecolor='none')
501
- # )
502
- # cumulative_height += count
503
-
504
- # # Display the plot in Streamlit
505
- # st.pyplot(fig)
506
-
507
- # # writeup
508
- # st.write("""
509
- # **What this visualization shows:**
510
- # This bar chart displays the distribution of complaints by Housing Block and Complaint Type. The data is stacked to show the percentage of complaints per block, categorized by type. This allows for a quick comparison of the most common complaint types across different housing blocks. While the percentages may be challenging to read when data for all blocks is displayed, they become more valuable and easier to interpret when a single block is selected. Selecting a specific block allows for clearer insights into the proportion of each complaint type within that block, providing more actionable information.
511
-
512
- # **Why it's interesting:**
513
- # By analyzing the distribution of complaints by both block and type, organizations can identify specific areas where certain complaint types are more prevalent. This insight helps target interventions and allocate resources more efficiently based on the most common issues in different housing blocks.
514
-
515
- # **Color Scheme:**
516
- # The 'inferno' color palette is used to represent different complaint types, with darker shades indicating a higher frequency of complaints. The stacked bar chart makes it easy to compare the distribution of complaints by block and type.
517
- # """)
518
- # In the above code , We incorporated all of the professor's suggestions and refined the chart to make it more useful for analysis while ensuring good aesthetics. Given that the data from block 3400 onwards is very sparse, we decided to exclude these records. This adjustment helped focus the visualization on the more relevant data, providing clearer insights and improving its overall effectiveness for analysis.
519
-
520
  elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestions Based on Professor's Feedback)":
521
- st.subheader("Complaints by Housing Block and Type - Incorporating Suggestions Based on Professor's Feedback")
522
 
523
- # Define blocks to be excluded
524
- excluded_blocks = [
525
- '3400 block', '3500 block', '3600 block', '3700 block', '3800 block', '3900 block',
526
- '4000 block', '4100 block', '4200 block', '4300 block', '4400 block', '4500 block',
527
- '4600 block', '4700 block', '4800 block', '4900 block', '5000 block'
528
- ]
529
-
530
- # Creating the desired order, excluding unwanted blocks
531
- desired_order = [
532
- '1 block', '100 block', '200 block', '300 block', '400 block', '500 block',
533
- '600 block', '700 block', '800 block', '900 block', '1000 block', '1100 block',
534
- '1200 block', '1300 block', '1400 block', '1500 block', '1600 block',
535
- '1700 block', '1800 block', '1900 block', '2000 block', '2100 block',
536
- '2200 block', '2300 block', '2400 block', '2500 block', '2600 block',
537
- '2700 block', '2800 block', '2900 block', '3000 block', '3100 block',
538
- '3200 block', '3300 block'
539
- ]
540
-
541
- # Filtering the data based on selected year
542
  filtered_data_time = data # Use filtered_data if date range is not needed
543
  if selected_year != 'All Time':
544
  filtered_data_time = filtered_data_time[filtered_data_time['Year Reported'] == selected_year]
545
 
546
- # Exclude blocks from the data
547
- filtered_data_time = filtered_data_time[~filtered_data_time['Housing Block'].isin(excluded_blocks)]
548
-
549
- # Get the list of blocks excluding the unwanted ones
550
- available_blocks = sorted(filtered_data_time['Housing Block'].unique().tolist())
551
-
552
- # Dropdown for Housing Block (excluding unwanted blocks)
553
- block_options = ['All Blocks'] + available_blocks
554
- selected_block = st.sidebar.selectbox("Select Housing Block", options=block_options, key="block_select")
555
-
556
- # Further filtering by selected Housing Block (if applicable)
557
  if selected_block != 'All Blocks':
558
  filtered_data_time = filtered_data_time[filtered_data_time['Housing Block'] == selected_block]
559
 
560
- # Pivoting the data
561
  complaint_pivot = filtered_data_time.pivot_table(
562
  index='Housing Block',
563
  columns='Type of Complaint',
@@ -566,15 +456,25 @@ elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestion
566
  fill_value=0
567
  )
568
 
569
- # Ensure the pivot data is numeric for plotting
570
  complaint_pivot = complaint_pivot.astype(float)
571
 
572
- # Reordering the pivot table by the desired order (excluding unwanted blocks)
573
- complaint_pivot = complaint_pivot.reindex(desired_order)
 
 
 
 
 
 
 
 
 
 
 
574
 
575
- # If a specific block is selected, only show that block on the x-axis
576
- if selected_block != 'All Blocks':
577
- complaint_pivot = complaint_pivot.loc[[selected_block]]
578
 
579
  # Calculating percentages for each complaint type per housing block
580
  percentages = complaint_pivot.div(complaint_pivot.sum(axis=1), axis=0) * 100
@@ -607,8 +507,7 @@ elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestion
607
  # writeup
608
  st.write("""
609
  **What this visualization shows:**
610
- This bar chart displays the distribution of complaints by Housing Block and Complaint Type. The data is stacked to show the percentage distribution of complaints per block, categorized by type. This allows for a quick comparison of the most common complaint types across different housing blocks. While the percentages may be challenging to read when data for all blocks is displayed, they become more valuable and easier to interpret when a single block is selected. Selecting a specific block allows for clearer insights into the proportion of each complaint type within that block, providing more actionable information.
611
- Given that the data from block 3400 onwards is very sparse, we decided to exclude these records. This adjustment helped focus the visualization on the more relevant data, providing clearer insights and improving its overall effectiveness for analysis.
612
 
613
  **Why it's interesting:**
614
  By analyzing the distribution of complaints by both block and type, organizations can identify specific areas where certain complaint types are more prevalent. This insight helps target interventions and allocate resources more efficiently based on the most common issues in different housing blocks.
@@ -616,6 +515,107 @@ elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestion
616
  **Color Scheme:**
617
  The 'inferno' color palette is used to represent different complaint types, with darker shades indicating a higher frequency of complaints. The stacked bar chart makes it easy to compare the distribution of complaints by block and type.
618
  """)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
619
 
620
  # Footer
621
  st.markdown("---")
 
435
  The 'inferno' color palette is used to represent different complaint types, with darker shades indicating a higher frequency of complaints. The stacked bar chart makes it easy to compare the distribution of complaints by block and type.
436
  """)
437
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
438
  elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestions Based on Professor's Feedback)":
439
+ st.subheader("Complaints by Housing Block and Type- Incorporating Suggestions Based on Professor's Feedback")
440
 
441
+ # Filtering the data based on the selected year and housing block
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
442
  filtered_data_time = data # Use filtered_data if date range is not needed
443
  if selected_year != 'All Time':
444
  filtered_data_time = filtered_data_time[filtered_data_time['Year Reported'] == selected_year]
445
 
446
+ # Further filtering by Housing Block (if applicable)
 
 
 
 
 
 
 
 
 
 
447
  if selected_block != 'All Blocks':
448
  filtered_data_time = filtered_data_time[filtered_data_time['Housing Block'] == selected_block]
449
 
450
+ # Pivoting the data based on the filtered data
451
  complaint_pivot = filtered_data_time.pivot_table(
452
  index='Housing Block',
453
  columns='Type of Complaint',
 
456
  fill_value=0
457
  )
458
 
459
+ # Ensuring the pivoted data is numeric for plotting
460
  complaint_pivot = complaint_pivot.astype(float)
461
 
462
+ # Desired order for the housing blocks
463
+ desired_order = [
464
+ '1 block', '100 block', '200 block', '300 block', '400 block', '500 block',
465
+ '600 block', '700 block', '800 block', '900 block', '1000 block', '1100 block',
466
+ '1200 block', '1300 block', '1400 block', '1500 block', '1600 block',
467
+ '1700 block', '1800 block', '1900 block', '2000 block', '2100 block',
468
+ '2200 block', '2300 block', '2400 block', '2500 block', '2600 block',
469
+ '2700 block', '2800 block', '2900 block', '3000 block', '3100 block',
470
+ '3200 block', '3300 block', '3400 block', '3500 block', '3600 block',
471
+ '3700 block', '3800 block', '3900 block', '4000 block', '4100 block',
472
+ '4200 block', '4300 block', '4400 block', '4500 block', '4600 block',
473
+ '4700 block', '4800 block', '4900 block', '5000 block'
474
+ ]
475
 
476
+ # Reordering the index of the pivot table according to the desired order
477
+ complaint_pivot = complaint_pivot.reindex(desired_order)
 
478
 
479
  # Calculating percentages for each complaint type per housing block
480
  percentages = complaint_pivot.div(complaint_pivot.sum(axis=1), axis=0) * 100
 
507
  # writeup
508
  st.write("""
509
  **What this visualization shows:**
510
+ This bar chart displays the distribution of complaints by Housing Block and Complaint Type. The data is stacked to show the percentage of complaints per block, categorized by type. This allows for a quick comparison of the most common complaint types across different housing blocks. While the percentages may be challenging to read when data for all blocks is displayed, they become more valuable and easier to interpret when a single block is selected. Selecting a specific block allows for clearer insights into the proportion of each complaint type within that block, providing more actionable information.
 
511
 
512
  **Why it's interesting:**
513
  By analyzing the distribution of complaints by both block and type, organizations can identify specific areas where certain complaint types are more prevalent. This insight helps target interventions and allocate resources more efficiently based on the most common issues in different housing blocks.
 
515
  **Color Scheme:**
516
  The 'inferno' color palette is used to represent different complaint types, with darker shades indicating a higher frequency of complaints. The stacked bar chart makes it easy to compare the distribution of complaints by block and type.
517
  """)
518
+ # In the above code , We incorporated all of the professor's suggestions and refined the chart to make it more useful for analysis while ensuring good aesthetics. Given that the data from block 3400 onwards is very sparse, we decided to exclude these records. This adjustment helped focus the visualization on the more relevant data, providing clearer insights and improving its overall effectiveness for analysis.
519
+
520
+ # elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestions Based on Professor's Feedback)":
521
+ # st.subheader("Complaints by Housing Block and Type - Incorporating Suggestions Based on Professor's Feedback")
522
+
523
+ # # Define blocks to be excluded
524
+ # excluded_blocks = [
525
+ # '3400 block', '3500 block', '3600 block', '3700 block', '3800 block', '3900 block',
526
+ # '4000 block', '4100 block', '4200 block', '4300 block', '4400 block', '4500 block',
527
+ # '4600 block', '4700 block', '4800 block', '4900 block', '5000 block'
528
+ # ]
529
+
530
+ # # Creating the desired order, excluding unwanted blocks
531
+ # desired_order = [
532
+ # '1 block', '100 block', '200 block', '300 block', '400 block', '500 block',
533
+ # '600 block', '700 block', '800 block', '900 block', '1000 block', '1100 block',
534
+ # '1200 block', '1300 block', '1400 block', '1500 block', '1600 block',
535
+ # '1700 block', '1800 block', '1900 block', '2000 block', '2100 block',
536
+ # '2200 block', '2300 block', '2400 block', '2500 block', '2600 block',
537
+ # '2700 block', '2800 block', '2900 block', '3000 block', '3100 block',
538
+ # '3200 block', '3300 block'
539
+ # ]
540
+
541
+ # # Filtering the data based on selected year
542
+ # filtered_data_time = data # Use filtered_data if date range is not needed
543
+ # if selected_year != 'All Time':
544
+ # filtered_data_time = filtered_data_time[filtered_data_time['Year Reported'] == selected_year]
545
+
546
+ # # Exclude blocks from the data
547
+ # filtered_data_time = filtered_data_time[~filtered_data_time['Housing Block'].isin(excluded_blocks)]
548
+
549
+ # # Get the list of blocks excluding the unwanted ones
550
+ # available_blocks = sorted(filtered_data_time['Housing Block'].unique().tolist())
551
+
552
+ # # Dropdown for Housing Block (excluding unwanted blocks)
553
+ # block_options = ['All Blocks'] + available_blocks
554
+ # selected_block = st.sidebar.selectbox("Select Housing Block", options=block_options, key="block_select")
555
+
556
+ # # Further filtering by selected Housing Block (if applicable)
557
+ # if selected_block != 'All Blocks':
558
+ # filtered_data_time = filtered_data_time[filtered_data_time['Housing Block'] == selected_block]
559
+
560
+ # # Pivoting the data
561
+ # complaint_pivot = filtered_data_time.pivot_table(
562
+ # index='Housing Block',
563
+ # columns='Type of Complaint',
564
+ # values='Disposition',
565
+ # aggfunc='count',
566
+ # fill_value=0
567
+ # )
568
+
569
+ # # Ensure the pivot data is numeric for plotting
570
+ # complaint_pivot = complaint_pivot.astype(float)
571
+
572
+ # # Reordering the pivot table by the desired order (excluding unwanted blocks)
573
+ # complaint_pivot = complaint_pivot.reindex(desired_order)
574
+
575
+ # # If a specific block is selected, only show that block on the x-axis
576
+ # if selected_block != 'All Blocks':
577
+ # complaint_pivot = complaint_pivot.loc[[selected_block]]
578
+
579
+ # # Calculating percentages for each complaint type per housing block
580
+ # percentages = complaint_pivot.div(complaint_pivot.sum(axis=1), axis=0) * 100
581
+
582
+ # # Plotting the data
583
+ # fig = complaint_pivot.plot(kind='bar', stacked=True, colormap='inferno', figsize=(10, 6)).get_figure()
584
+
585
+ # # Adding percentage labels to the plot
586
+ # ax = fig.gca()
587
+ # for idx, block in enumerate(complaint_pivot.index):
588
+ # cumulative_height = 0
589
+ # for i, complaint_type in enumerate(complaint_pivot.columns):
590
+ # count = complaint_pivot.iloc[idx, i]
591
+ # percent = percentages.iloc[idx, i]
592
+ # if count > 0:
593
+ # # Compute the position for the percentage label
594
+ # x_pos = idx - 0.4 + 0.8 / 2 # Adjusting the position of the label
595
+ # y_pos = cumulative_height + count / 2
596
+ # ax.text(
597
+ # x_pos, y_pos, f"{percent:.1f}%",
598
+ # ha='center', va='center',
599
+ # fontsize=10, color='black',
600
+ # bbox=dict(facecolor='white', alpha=0.7, edgecolor='none')
601
+ # )
602
+ # cumulative_height += count
603
+
604
+ # # Display the plot in Streamlit
605
+ # st.pyplot(fig)
606
+
607
+ # # writeup
608
+ # st.write("""
609
+ # **What this visualization shows:**
610
+ # This bar chart displays the distribution of complaints by Housing Block and Complaint Type. The data is stacked to show the percentage distribution of complaints per block, categorized by type. This allows for a quick comparison of the most common complaint types across different housing blocks. While the percentages may be challenging to read when data for all blocks is displayed, they become more valuable and easier to interpret when a single block is selected. Selecting a specific block allows for clearer insights into the proportion of each complaint type within that block, providing more actionable information.
611
+ # Given that the data from block 3400 onwards is very sparse, we decided to exclude these records. This adjustment helped focus the visualization on the more relevant data, providing clearer insights and improving its overall effectiveness for analysis.
612
+
613
+ # **Why it's interesting:**
614
+ # By analyzing the distribution of complaints by both block and type, organizations can identify specific areas where certain complaint types are more prevalent. This insight helps target interventions and allocate resources more efficiently based on the most common issues in different housing blocks.
615
+
616
+ # **Color Scheme:**
617
+ # The 'inferno' color palette is used to represent different complaint types, with darker shades indicating a higher frequency of complaints. The stacked bar chart makes it easy to compare the distribution of complaints by block and type.
618
+ # """)
619
 
620
  # Footer
621
  st.markdown("---")