Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -138,11 +138,11 @@ with col2:
|
|
| 138 |
if viz_type == "Complaint Types":
|
| 139 |
st.subheader("Top 5 Complaint Types Pie Chart")
|
| 140 |
|
| 141 |
-
#
|
| 142 |
top_complaints = filtered_data['Type of Complaint'].value_counts().nlargest(5).reset_index()
|
| 143 |
top_complaints.columns = ['Complaint Type', 'Count']
|
| 144 |
|
| 145 |
-
#
|
| 146 |
fig = px.pie(
|
| 147 |
top_complaints,
|
| 148 |
names='Complaint Type',
|
|
@@ -154,10 +154,10 @@ if viz_type == "Complaint Types":
|
|
| 154 |
)
|
| 155 |
fig.update_traces(textinfo='percent+label', hovertemplate='<b>%{label}</b><br>Complaints: %{value}<br>Percentage: %{percent}')
|
| 156 |
|
| 157 |
-
#
|
| 158 |
st.plotly_chart(fig, use_container_width=True)
|
| 159 |
|
| 160 |
-
#
|
| 161 |
st.write("""
|
| 162 |
**What this visualization shows:**
|
| 163 |
This interactive pie chart displays the distribution of the top 5 complaint types by year.
|
|
@@ -170,13 +170,13 @@ if viz_type == "Complaint Types":
|
|
| 170 |
elif viz_type == "Geographic Distribution":
|
| 171 |
st.subheader("Clustered Heatmap of Complaints")
|
| 172 |
|
| 173 |
-
#
|
| 174 |
map_center = [filtered_data['Latitude'].mean(), filtered_data['Longitude'].mean()]
|
| 175 |
m = folium.Map(location=map_center, zoom_start=12)
|
| 176 |
heat_data = filtered_data[['Latitude', 'Longitude']].dropna().values.tolist()
|
| 177 |
HeatMap(heat_data).add_to(m)
|
| 178 |
|
| 179 |
-
#
|
| 180 |
st_folium(m, width=700, height=500)
|
| 181 |
|
| 182 |
# Write-up
|
|
@@ -218,22 +218,22 @@ elif viz_type == "Geographic Distribution":
|
|
| 218 |
elif viz_type == "Submission Methods":
|
| 219 |
st.subheader("Submission Methods Analysis")
|
| 220 |
|
| 221 |
-
#
|
| 222 |
plot_type = st.selectbox("Select Plot Type", options=["Bar Chart", "Pie Chart"])
|
| 223 |
|
| 224 |
-
#
|
| 225 |
submission_counts = filtered_data['Method Submitted'].value_counts().nlargest(5)
|
| 226 |
submission_data = submission_counts.reset_index()
|
| 227 |
submission_data.columns = ['Submission Method', 'Count']
|
| 228 |
|
| 229 |
if plot_type == "Bar Chart":
|
| 230 |
-
#
|
| 231 |
fig, ax = plt.subplots(figsize=(10, 6))
|
| 232 |
sns.barplot(x=submission_data['Count'], y=submission_data['Submission Method'], palette='inferno', ax=ax)
|
| 233 |
st.pyplot(fig)
|
| 234 |
|
| 235 |
elif plot_type == "Pie Chart":
|
| 236 |
-
#
|
| 237 |
fig = px.pie(
|
| 238 |
submission_data,
|
| 239 |
names='Submission Method',
|
|
@@ -247,7 +247,7 @@ elif viz_type == "Submission Methods":
|
|
| 247 |
fig.update_traces(textinfo='percent+label', hovertemplate='<b>%{label}</b><br>Complaints: %{value}<br>Percentage: %{percent}')
|
| 248 |
st.plotly_chart(fig, use_container_width=True)
|
| 249 |
|
| 250 |
-
#
|
| 251 |
st.write("""
|
| 252 |
**What this visualization shows:**
|
| 253 |
This chart shows the number of complaints submitted via different methods, such as email, phone, online form, etc., with a focus on the top 5 submission methods.
|
|
@@ -281,10 +281,10 @@ elif viz_type == "Submission Methods":
|
|
| 281 |
elif viz_type == "Complaints by Disposition":
|
| 282 |
st.subheader("Complaints by Disposition")
|
| 283 |
|
| 284 |
-
#
|
| 285 |
disposition_by_complaint_type = filtered_data.groupby(['Type of Complaint', 'Disposition']).size().unstack(fill_value=0)
|
| 286 |
|
| 287 |
-
#
|
| 288 |
fig, ax = plt.subplots(figsize=(12, 8))
|
| 289 |
disposition_by_complaint_type.plot(kind='bar', stacked=True, colormap='inferno', ax=ax)
|
| 290 |
|
|
@@ -292,7 +292,7 @@ elif viz_type == "Complaints by Disposition":
|
|
| 292 |
ax.set_ylabel("Number of Complaints")
|
| 293 |
ax.set_xlabel("Complaint Type")
|
| 294 |
|
| 295 |
-
#
|
| 296 |
st.pyplot(fig)
|
| 297 |
|
| 298 |
st.write("""
|
|
@@ -372,16 +372,16 @@ elif viz_type == "Complaints Over Time":
|
|
| 372 |
elif viz_type == "Complaints by Housing Block and Type":
|
| 373 |
st.subheader("Complaints by Housing Block and Type")
|
| 374 |
|
| 375 |
-
#
|
| 376 |
filtered_data_time = filtered_data # Use filtered_data if date range is not needed
|
| 377 |
if selected_year != 'All Time':
|
| 378 |
filtered_data_time = filtered_data_time[filtered_data_time['Year Reported'] == selected_year]
|
| 379 |
|
| 380 |
-
# Further
|
| 381 |
if selected_block != 'All Blocks':
|
| 382 |
filtered_data_time = filtered_data_time[filtered_data_time['Housing Block'] == selected_block]
|
| 383 |
|
| 384 |
-
#
|
| 385 |
complaint_pivot = filtered_data_time.pivot_table(
|
| 386 |
index='Housing Block',
|
| 387 |
columns='Type of Complaint',
|
|
@@ -390,10 +390,10 @@ elif viz_type == "Complaints by Housing Block and Type":
|
|
| 390 |
fill_value=0
|
| 391 |
)
|
| 392 |
|
| 393 |
-
#
|
| 394 |
complaint_pivot = complaint_pivot.astype(float)
|
| 395 |
|
| 396 |
-
#
|
| 397 |
fig = complaint_pivot.plot(kind='bar', stacked=True, colormap='inferno', figsize=(10, 6)).get_figure()
|
| 398 |
st.pyplot(fig)
|
| 399 |
|
|
|
|
| 138 |
if viz_type == "Complaint Types":
|
| 139 |
st.subheader("Top 5 Complaint Types Pie Chart")
|
| 140 |
|
| 141 |
+
# Preparing data: Select the top 5 complaint types
|
| 142 |
top_complaints = filtered_data['Type of Complaint'].value_counts().nlargest(5).reset_index()
|
| 143 |
top_complaints.columns = ['Complaint Type', 'Count']
|
| 144 |
|
| 145 |
+
# Creating an interactive pie chart with the 'inferno' color scheme
|
| 146 |
fig = px.pie(
|
| 147 |
top_complaints,
|
| 148 |
names='Complaint Type',
|
|
|
|
| 154 |
)
|
| 155 |
fig.update_traces(textinfo='percent+label', hovertemplate='<b>%{label}</b><br>Complaints: %{value}<br>Percentage: %{percent}')
|
| 156 |
|
| 157 |
+
# Displaying chart
|
| 158 |
st.plotly_chart(fig, use_container_width=True)
|
| 159 |
|
| 160 |
+
# write-up
|
| 161 |
st.write("""
|
| 162 |
**What this visualization shows:**
|
| 163 |
This interactive pie chart displays the distribution of the top 5 complaint types by year.
|
|
|
|
| 170 |
elif viz_type == "Geographic Distribution":
|
| 171 |
st.subheader("Clustered Heatmap of Complaints")
|
| 172 |
|
| 173 |
+
# Generating the heatmap
|
| 174 |
map_center = [filtered_data['Latitude'].mean(), filtered_data['Longitude'].mean()]
|
| 175 |
m = folium.Map(location=map_center, zoom_start=12)
|
| 176 |
heat_data = filtered_data[['Latitude', 'Longitude']].dropna().values.tolist()
|
| 177 |
HeatMap(heat_data).add_to(m)
|
| 178 |
|
| 179 |
+
# Displaying the map
|
| 180 |
st_folium(m, width=700, height=500)
|
| 181 |
|
| 182 |
# Write-up
|
|
|
|
| 218 |
elif viz_type == "Submission Methods":
|
| 219 |
st.subheader("Submission Methods Analysis")
|
| 220 |
|
| 221 |
+
# Allowing the user to select the type of chart (Bar or Pie)
|
| 222 |
plot_type = st.selectbox("Select Plot Type", options=["Bar Chart", "Pie Chart"])
|
| 223 |
|
| 224 |
+
# Getting the top 5 submission methods
|
| 225 |
submission_counts = filtered_data['Method Submitted'].value_counts().nlargest(5)
|
| 226 |
submission_data = submission_counts.reset_index()
|
| 227 |
submission_data.columns = ['Submission Method', 'Count']
|
| 228 |
|
| 229 |
if plot_type == "Bar Chart":
|
| 230 |
+
# Creating a bar chart with Seaborn
|
| 231 |
fig, ax = plt.subplots(figsize=(10, 6))
|
| 232 |
sns.barplot(x=submission_data['Count'], y=submission_data['Submission Method'], palette='inferno', ax=ax)
|
| 233 |
st.pyplot(fig)
|
| 234 |
|
| 235 |
elif plot_type == "Pie Chart":
|
| 236 |
+
# Creating an interactive pie chart with Plotly
|
| 237 |
fig = px.pie(
|
| 238 |
submission_data,
|
| 239 |
names='Submission Method',
|
|
|
|
| 247 |
fig.update_traces(textinfo='percent+label', hovertemplate='<b>%{label}</b><br>Complaints: %{value}<br>Percentage: %{percent}')
|
| 248 |
st.plotly_chart(fig, use_container_width=True)
|
| 249 |
|
| 250 |
+
# Write-up
|
| 251 |
st.write("""
|
| 252 |
**What this visualization shows:**
|
| 253 |
This chart shows the number of complaints submitted via different methods, such as email, phone, online form, etc., with a focus on the top 5 submission methods.
|
|
|
|
| 281 |
elif viz_type == "Complaints by Disposition":
|
| 282 |
st.subheader("Complaints by Disposition")
|
| 283 |
|
| 284 |
+
# Grouping by Complaint Type and Disposition to make the analysis more complex
|
| 285 |
disposition_by_complaint_type = filtered_data.groupby(['Type of Complaint', 'Disposition']).size().unstack(fill_value=0)
|
| 286 |
|
| 287 |
+
# Plotting the bar chart with the 'inferno' color scheme
|
| 288 |
fig, ax = plt.subplots(figsize=(12, 8))
|
| 289 |
disposition_by_complaint_type.plot(kind='bar', stacked=True, colormap='inferno', ax=ax)
|
| 290 |
|
|
|
|
| 292 |
ax.set_ylabel("Number of Complaints")
|
| 293 |
ax.set_xlabel("Complaint Type")
|
| 294 |
|
| 295 |
+
# Displaying the plot
|
| 296 |
st.pyplot(fig)
|
| 297 |
|
| 298 |
st.write("""
|
|
|
|
| 372 |
elif viz_type == "Complaints by Housing Block and Type":
|
| 373 |
st.subheader("Complaints by Housing Block and Type")
|
| 374 |
|
| 375 |
+
# Filtering the data based on the selected year and housing block
|
| 376 |
filtered_data_time = filtered_data # Use filtered_data if date range is not needed
|
| 377 |
if selected_year != 'All Time':
|
| 378 |
filtered_data_time = filtered_data_time[filtered_data_time['Year Reported'] == selected_year]
|
| 379 |
|
| 380 |
+
# Further filtering by Housing Block (if applicable)
|
| 381 |
if selected_block != 'All Blocks':
|
| 382 |
filtered_data_time = filtered_data_time[filtered_data_time['Housing Block'] == selected_block]
|
| 383 |
|
| 384 |
+
# Pivoting the data based on the filtered data
|
| 385 |
complaint_pivot = filtered_data_time.pivot_table(
|
| 386 |
index='Housing Block',
|
| 387 |
columns='Type of Complaint',
|
|
|
|
| 390 |
fill_value=0
|
| 391 |
)
|
| 392 |
|
| 393 |
+
# Ensuring the pivoted data is numeric for plotting
|
| 394 |
complaint_pivot = complaint_pivot.astype(float)
|
| 395 |
|
| 396 |
+
# Plotting the data
|
| 397 |
fig = complaint_pivot.plot(kind='bar', stacked=True, colormap='inferno', figsize=(10, 6)).get_figure()
|
| 398 |
st.pyplot(fig)
|
| 399 |
|