ralate2 commited on
Commit
ed54ea1
·
verified ·
1 Parent(s): 4b8004d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -139
app.py CHANGED
@@ -101,7 +101,10 @@ if viz_type in ["Complaints by Housing Block and Type", "Complaints by Housing B
101
  # options=block_options,
102
  # key=f"block_select_{viz_type}" # Unique key for each visualization
103
  # )
104
- valid_blocks = [block for block in data['Housing Block'].unique() if int(block.split()[0]) < 3400]
 
 
 
105
  block_options = ['All Blocks'] + sorted(valid_blocks)
106
  selected_block = st.sidebar.selectbox("Select Housing Block", options=block_options, key="block_select")
107
 
@@ -438,86 +441,86 @@ elif viz_type == "Complaints by Housing Block and Type":
438
  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.
439
  """)
440
 
441
- # elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestions Based on Professor's Feedback)":
442
- # st.subheader("Complaints by Housing Block and Type- Incorporating Suggestions Based on Professor's Feedback")
443
 
444
- # # Filtering the data based on the selected year and housing block
445
- # filtered_data_time = data # Use filtered_data if date range is not needed
446
- # if selected_year != 'All Time':
447
- # filtered_data_time = filtered_data_time[filtered_data_time['Year Reported'] == selected_year]
448
 
449
- # # Further filtering by Housing Block (if applicable)
450
- # if selected_block != 'All Blocks':
451
- # filtered_data_time = filtered_data_time[filtered_data_time['Housing Block'] == selected_block]
452
 
453
- # # Pivoting the data based on the filtered data
454
- # complaint_pivot = filtered_data_time.pivot_table(
455
- # index='Housing Block',
456
- # columns='Type of Complaint',
457
- # values='Disposition',
458
- # aggfunc='count',
459
- # fill_value=0
460
- # )
461
 
462
- # # Ensuring the pivoted data is numeric for plotting
463
- # complaint_pivot = complaint_pivot.astype(float)
464
 
465
- # # Desired order for the housing blocks
466
- # desired_order = [
467
- # '1 block', '100 block', '200 block', '300 block', '400 block', '500 block',
468
- # '600 block', '700 block', '800 block', '900 block', '1000 block', '1100 block',
469
- # '1200 block', '1300 block', '1400 block', '1500 block', '1600 block',
470
- # '1700 block', '1800 block', '1900 block', '2000 block', '2100 block',
471
- # '2200 block', '2300 block', '2400 block', '2500 block', '2600 block',
472
- # '2700 block', '2800 block', '2900 block', '3000 block', '3100 block',
473
- # '3200 block', '3300 block', '3400 block', '3500 block', '3600 block',
474
- # '3700 block', '3800 block', '3900 block', '4000 block', '4100 block',
475
- # '4200 block', '4300 block', '4400 block', '4500 block', '4600 block',
476
- # '4700 block', '4800 block', '4900 block', '5000 block'
477
- # ]
478
 
479
- # # Reordering the index of the pivot table according to the desired order
480
- # complaint_pivot = complaint_pivot.reindex(desired_order)
481
 
482
- # # Calculating percentages for each complaint type per housing block
483
- # percentages = complaint_pivot.div(complaint_pivot.sum(axis=1), axis=0) * 100
484
 
485
- # # Plotting the data
486
- # fig = complaint_pivot.plot(kind='bar', stacked=True, colormap='inferno', figsize=(10, 6)).get_figure()
487
 
488
- # # Adding percentage labels to the plot
489
- # ax = fig.gca()
490
- # for idx, block in enumerate(complaint_pivot.index):
491
- # cumulative_height = 0
492
- # for i, complaint_type in enumerate(complaint_pivot.columns):
493
- # count = complaint_pivot.iloc[idx, i]
494
- # percent = percentages.iloc[idx, i]
495
- # if count > 0:
496
- # # Compute the position for the percentage label
497
- # x_pos = idx - 0.4 + 0.8 / 2 # Adjusting the position of the label
498
- # y_pos = cumulative_height + count / 2
499
- # ax.text(
500
- # x_pos, y_pos, f"{percent:.1f}%",
501
- # ha='center', va='center',
502
- # fontsize=10, color='black',
503
- # bbox=dict(facecolor='white', alpha=0.7, edgecolor='none')
504
- # )
505
- # cumulative_height += count
506
 
507
- # # Display the plot in Streamlit
508
- # st.pyplot(fig)
509
 
510
- # # writeup
511
- # st.write("""
512
- # **What this visualization shows:**
513
- # 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.
514
 
515
- # **Why it's interesting:**
516
- # 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.
517
 
518
- # **Color Scheme:**
519
- # 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.
520
- # """)
521
  # 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.
522
 
523
  # elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestions Based on Professor's Feedback)":
@@ -620,77 +623,6 @@ elif viz_type == "Complaints by Housing Block and Type":
620
  # 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.
621
  # """)
622
 
623
- elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestions Based on Professor's Feedback)":
624
- st.subheader("Complaints by Housing Block and Type - Incorporating Suggestions Based on Professor's Feedback")
625
-
626
- # Filtering the data based on the selected year and housing block
627
- filtered_data_time = data # Use filtered_data if date range is not needed
628
- if selected_year != 'All Time':
629
- filtered_data_time = filtered_data_time[filtered_data_time['Year Reported'] == selected_year]
630
-
631
- # Exclude blocks from 3400 onwards
632
- valid_blocks = [block for block in filtered_data_time['Housing Block'].unique() if int(block.split()[0]) < 3400]
633
- filtered_data_time = filtered_data_time[filtered_data_time['Housing Block'].isin(valid_blocks)]
634
-
635
- # Further filtering by Housing Block (if applicable)
636
- if selected_block != 'All Blocks':
637
- filtered_data_time = filtered_data_time[filtered_data_time['Housing Block'] == selected_block]
638
-
639
- # Pivoting the data based on the filtered data
640
- complaint_pivot = filtered_data_time.pivot_table(
641
- index='Housing Block',
642
- columns='Type of Complaint',
643
- values='Disposition',
644
- aggfunc='count',
645
- fill_value=0
646
- )
647
-
648
- # Ensuring the pivoted data is numeric for plotting
649
- complaint_pivot = complaint_pivot.astype(float)
650
-
651
- # Show only the selected block on the X-axis if a specific block is chosen
652
- if selected_block != 'All Blocks':
653
- complaint_pivot = complaint_pivot.loc[[selected_block]]
654
-
655
- # Calculating percentages for each complaint type per housing block
656
- percentages = complaint_pivot.div(complaint_pivot.sum(axis=1), axis=0) * 100
657
-
658
- # Plotting the data
659
- fig = complaint_pivot.plot(kind='bar', stacked=True, colormap='inferno', figsize=(10, 6)).get_figure()
660
-
661
- # Adding percentage labels to the plot
662
- ax = fig.gca()
663
- for idx, block in enumerate(complaint_pivot.index):
664
- cumulative_height = 0
665
- for i, complaint_type in enumerate(complaint_pivot.columns):
666
- count = complaint_pivot.iloc[idx, i]
667
- percent = percentages.iloc[idx, i]
668
- if count > 0:
669
- # Compute the position for the percentage label
670
- x_pos = idx # Adjusting the position of the label
671
- y_pos = cumulative_height + count / 2
672
- ax.text(
673
- x_pos, y_pos, f"{percent:.1f}%",
674
- ha='center', va='center',
675
- fontsize=10, color='black',
676
- bbox=dict(facecolor='white', alpha=0.7, edgecolor='none')
677
- )
678
- cumulative_height += count
679
-
680
- # Display the plot in Streamlit
681
- st.pyplot(fig)
682
-
683
- # Writeup
684
- st.write("""
685
- **What this visualization shows:**
686
- 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. Selecting a specific block allows for clearer insights into the proportion of each complaint type within that block, providing more actionable information.
687
-
688
- **Why it's interesting:**
689
- 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.
690
-
691
- **Color Scheme:**
692
- 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.
693
- """)
694
 
695
  # Footer
696
  st.markdown("---")
 
101
  # options=block_options,
102
  # key=f"block_select_{viz_type}" # Unique key for each visualization
103
  # )
104
+ valid_blocks = [
105
+ block for block in data['Housing Block'].unique()
106
+ if block.split()[0].isdigit() and int(block.split()[0]) < 3400
107
+ ]
108
  block_options = ['All Blocks'] + sorted(valid_blocks)
109
  selected_block = st.sidebar.selectbox("Select Housing Block", options=block_options, key="block_select")
110
 
 
441
  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.
442
  """)
443
 
444
+ elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestions Based on Professor's Feedback)":
445
+ st.subheader("Complaints by Housing Block and Type- Incorporating Suggestions Based on Professor's Feedback")
446
 
447
+ # Filtering the data based on the selected year and housing block
448
+ filtered_data_time = data # Use filtered_data if date range is not needed
449
+ if selected_year != 'All Time':
450
+ filtered_data_time = filtered_data_time[filtered_data_time['Year Reported'] == selected_year]
451
 
452
+ # Further filtering by Housing Block (if applicable)
453
+ if selected_block != 'All Blocks':
454
+ filtered_data_time = filtered_data_time[filtered_data_time['Housing Block'] == selected_block]
455
 
456
+ # Pivoting the data based on the filtered data
457
+ complaint_pivot = filtered_data_time.pivot_table(
458
+ index='Housing Block',
459
+ columns='Type of Complaint',
460
+ values='Disposition',
461
+ aggfunc='count',
462
+ fill_value=0
463
+ )
464
 
465
+ # Ensuring the pivoted data is numeric for plotting
466
+ complaint_pivot = complaint_pivot.astype(float)
467
 
468
+ # Desired order for the housing blocks
469
+ desired_order = [
470
+ '1 block', '100 block', '200 block', '300 block', '400 block', '500 block',
471
+ '600 block', '700 block', '800 block', '900 block', '1000 block', '1100 block',
472
+ '1200 block', '1300 block', '1400 block', '1500 block', '1600 block',
473
+ '1700 block', '1800 block', '1900 block', '2000 block', '2100 block',
474
+ '2200 block', '2300 block', '2400 block', '2500 block', '2600 block',
475
+ '2700 block', '2800 block', '2900 block', '3000 block', '3100 block',
476
+ '3200 block', '3300 block', '3400 block', '3500 block', '3600 block',
477
+ '3700 block', '3800 block', '3900 block', '4000 block', '4100 block',
478
+ '4200 block', '4300 block', '4400 block', '4500 block', '4600 block',
479
+ '4700 block', '4800 block', '4900 block', '5000 block'
480
+ ]
481
 
482
+ # Reordering the index of the pivot table according to the desired order
483
+ complaint_pivot = complaint_pivot.reindex(desired_order)
484
 
485
+ # Calculating percentages for each complaint type per housing block
486
+ percentages = complaint_pivot.div(complaint_pivot.sum(axis=1), axis=0) * 100
487
 
488
+ # Plotting the data
489
+ fig = complaint_pivot.plot(kind='bar', stacked=True, colormap='inferno', figsize=(10, 6)).get_figure()
490
 
491
+ # Adding percentage labels to the plot
492
+ ax = fig.gca()
493
+ for idx, block in enumerate(complaint_pivot.index):
494
+ cumulative_height = 0
495
+ for i, complaint_type in enumerate(complaint_pivot.columns):
496
+ count = complaint_pivot.iloc[idx, i]
497
+ percent = percentages.iloc[idx, i]
498
+ if count > 0:
499
+ # Compute the position for the percentage label
500
+ x_pos = idx - 0.4 + 0.8 / 2 # Adjusting the position of the label
501
+ y_pos = cumulative_height + count / 2
502
+ ax.text(
503
+ x_pos, y_pos, f"{percent:.1f}%",
504
+ ha='center', va='center',
505
+ fontsize=10, color='black',
506
+ bbox=dict(facecolor='white', alpha=0.7, edgecolor='none')
507
+ )
508
+ cumulative_height += count
509
 
510
+ # Display the plot in Streamlit
511
+ st.pyplot(fig)
512
 
513
+ # writeup
514
+ st.write("""
515
+ **What this visualization shows:**
516
+ 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.
517
 
518
+ **Why it's interesting:**
519
+ 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.
520
 
521
+ **Color Scheme:**
522
+ 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.
523
+ """)
524
  # 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.
525
 
526
  # elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestions Based on Professor's Feedback)":
 
623
  # 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.
624
  # """)
625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
626
 
627
  # Footer
628
  st.markdown("---")