ralate2 commited on
Commit
1a4102e
·
verified ·
1 Parent(s): fac1189

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -13
app.py CHANGED
@@ -77,9 +77,28 @@ viz_type = st.sidebar.selectbox("Select Visualization", [
77
  "Top Complaint Types", "Complaints Over Time", "Complaints by Housing Block and Type"
78
  ])
79
 
80
- # Filter data based on year
 
 
 
 
 
 
 
 
81
  filtered_data = data if selected_year == 'All Time' else data[data['Year Reported'] == selected_year]
82
 
 
 
 
 
 
 
 
 
 
 
 
83
  st.header(f"Analysis for {'All Time' if selected_year == 'All Time' else selected_year}")
84
 
85
  # Display metrics
@@ -247,12 +266,10 @@ elif viz_type == "Top Complaint Types":
247
 
248
  elif viz_type == "Complaints Over Time":
249
  st.subheader("Complaints Over Time")
250
- complaints_over_time = filtered_data.groupby(filtered_data['Date Reported'].dt.date).size()
251
- fig, ax = plt.subplots()
252
 
253
- # Apply color scheme to the plot
254
  ax.plot(complaints_over_time.index, complaints_over_time.values, marker='o', color='tab:blue')
255
-
256
  ax.set_title("Complaints Over Time")
257
  st.pyplot(fig)
258
 
@@ -262,17 +279,29 @@ elif viz_type == "Complaints Over Time":
262
  The use of a blue color scheme highlights the flow and continuity of the data, providing a clear view of the patterns over time.
263
  """)
264
 
265
- elif viz_type == "Complaints by Housing Block and Type":
266
  st.subheader("Complaints by Housing Block and Type")
267
- complaint_pivot = filtered_data.pivot_table(
268
- index='Housing Block',
269
- columns='Type of Complaint',
270
- values='Disposition',
271
- aggfunc='count',
272
- fill_value=0
273
- )
 
 
 
 
 
 
 
 
 
 
 
274
  fig = complaint_pivot.plot(kind='bar', stacked=True, colormap='inferno', figsize=(10, 6)).get_figure()
275
  st.pyplot(fig)
 
276
  st.write("""
277
  **What this visualization shows:**
278
  This line chart shows the trend of complaints over time, displaying the number of complaints reported for each day. It helps identify patterns, peaks, and trends in the complaints data.
 
77
  "Top Complaint Types", "Complaints Over Time", "Complaints by Housing Block and Type"
78
  ])
79
 
80
+ # Date Range Selector for Complaints Over Time
81
+ start_date = st.sidebar.date_input("Start Date", pd.to_datetime("2020-01-01"))
82
+ end_date = st.sidebar.date_input("End Date", pd.to_datetime("2024-12-31"))
83
+
84
+ # Dropdown for Housing Block
85
+ block_options = ['All Blocks'] + sorted(data['Housing Block'].unique().tolist())
86
+ selected_block = st.sidebar.selectbox("Select Housing Block", options=block_options)
87
+
88
+ # Filter data based on selected year
89
  filtered_data = data if selected_year == 'All Time' else data[data['Year Reported'] == selected_year]
90
 
91
+ # Filter data based on date range
92
+ filtered_data_time = filtered_data[
93
+ (filtered_data['Date Reported'] >= pd.to_datetime(start_date)) &
94
+ (filtered_data['Date Reported'] <= pd.to_datetime(end_date))
95
+ ]
96
+
97
+ # Filter data based on selected housing block
98
+ if selected_block != 'All Blocks':
99
+ filtered_data_time = filtered_data_time[filtered_data_time['Housing Block'] == selected_block]
100
+
101
+ # Header for selected year
102
  st.header(f"Analysis for {'All Time' if selected_year == 'All Time' else selected_year}")
103
 
104
  # Display metrics
 
266
 
267
  elif viz_type == "Complaints Over Time":
268
  st.subheader("Complaints Over Time")
269
+ complaints_over_time = filtered_data_time.groupby(filtered_data_time['Date Reported'].dt.date).size()
 
270
 
271
+ fig, ax = plt.subplots()
272
  ax.plot(complaints_over_time.index, complaints_over_time.values, marker='o', color='tab:blue')
 
273
  ax.set_title("Complaints Over Time")
274
  st.pyplot(fig)
275
 
 
279
  The use of a blue color scheme highlights the flow and continuity of the data, providing a clear view of the patterns over time.
280
  """)
281
 
282
+ elif iz_type == "Complaints by Housing Block and Type":
283
  st.subheader("Complaints by Housing Block and Type")
284
+
285
+ if housing_block != 'All Blocks':
286
+ complaint_pivot = filtered_data_time.pivot_table(
287
+ index='Housing Block',
288
+ columns='Type of Complaint',
289
+ values='Disposition',
290
+ aggfunc='count',
291
+ fill_value=0
292
+ )
293
+ else:
294
+ complaint_pivot = filtered_data.pivot_table(
295
+ index='Housing Block',
296
+ columns='Type of Complaint',
297
+ values='Disposition',
298
+ aggfunc='count',
299
+ fill_value=0
300
+ )
301
+
302
  fig = complaint_pivot.plot(kind='bar', stacked=True, colormap='inferno', figsize=(10, 6)).get_figure()
303
  st.pyplot(fig)
304
+
305
  st.write("""
306
  **What this visualization shows:**
307
  This line chart shows the trend of complaints over time, displaying the number of complaints reported for each day. It helps identify patterns, peaks, and trends in the complaints data.