Update src/streamlit_app.py
Browse files- src/streamlit_app.py +8 -16
src/streamlit_app.py
CHANGED
|
@@ -91,29 +91,21 @@ st.altair_chart(chart1, use_container_width=True)
|
|
| 91 |
###############################################################
|
| 92 |
|
| 93 |
import plotly.express as px
|
| 94 |
-
# ββ 3. Parse year from date ββ
|
| 95 |
-
# Detect the column containing βdateβ
|
| 96 |
-
date_col = next((c for c in df.columns if "date" in c.lower()), None)
|
| 97 |
-
if date_col is None:
|
| 98 |
-
st.error("Could not find a date column in the dataset.")
|
| 99 |
-
st.stop()
|
| 100 |
-
|
| 101 |
-
df[date_col] = pd.to_datetime(df[date_col], errors="coerce")
|
| 102 |
-
df["Year"] = df[date_col].dt.year
|
| 103 |
|
| 104 |
-
# ββ
|
| 105 |
-
years = sorted(df["
|
| 106 |
selected_year = st.selectbox("Select Year", years, index=len(years)-1)
|
| 107 |
|
| 108 |
-
|
|
|
|
| 109 |
|
| 110 |
# ββ 5. Compute top 10 crime types for that year ββ
|
| 111 |
top_crimes = (
|
| 112 |
filtered["crm_cd_desc"]
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
)
|
| 118 |
top_crimes["Percentage"] = top_crimes["Count"] / top_crimes["Count"].sum()
|
| 119 |
|
|
|
|
| 91 |
###############################################################
|
| 92 |
|
| 93 |
import plotly.express as px
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
+
# ββ 3. Year filter (above chart) ββ
|
| 96 |
+
years = sorted(df["year"].dropna().astype(int).unique())
|
| 97 |
selected_year = st.selectbox("Select Year", years, index=len(years)-1)
|
| 98 |
|
| 99 |
+
# ββ 4. Filter data for the selected year ββ
|
| 100 |
+
filtered = df[df["year"] == selected_year]
|
| 101 |
|
| 102 |
# ββ 5. Compute top 10 crime types for that year ββ
|
| 103 |
top_crimes = (
|
| 104 |
filtered["crm_cd_desc"]
|
| 105 |
+
.value_counts()
|
| 106 |
+
.nlargest(10)
|
| 107 |
+
.rename_axis("Crime Type")
|
| 108 |
+
.reset_index(name="Count")
|
| 109 |
)
|
| 110 |
top_crimes["Percentage"] = top_crimes["Count"] / top_crimes["Count"].sum()
|
| 111 |
|