Spaces:
Runtime error
Runtime error
Update app.py
#1
by krisin - opened
app.py
CHANGED
|
@@ -14,7 +14,8 @@ st.sidebar.title("Navigation")
|
|
| 14 |
page = st.sidebar.radio("Go to", [
|
| 15 |
"Introduction",
|
| 16 |
"Visual 1&2",
|
| 17 |
-
"Visual 3",
|
|
|
|
| 18 |
"Conclusion"
|
| 19 |
])
|
| 20 |
|
|
@@ -277,6 +278,71 @@ elif page == "Visual 3":
|
|
| 277 |
This raw count is useful for identifying where the largest absolute numbers of active travelers are located, but it should be interpreted with population context in mind.
|
| 278 |
""")
|
| 279 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
# ----------------------------
|
| 281 |
# Page: Conclusion
|
| 282 |
# ----------------------------
|
|
|
|
| 14 |
page = st.sidebar.radio("Go to", [
|
| 15 |
"Introduction",
|
| 16 |
"Visual 1&2",
|
| 17 |
+
"Visual 3",
|
| 18 |
+
"Visual 4",
|
| 19 |
"Conclusion"
|
| 20 |
])
|
| 21 |
|
|
|
|
| 278 |
This raw count is useful for identifying where the largest absolute numbers of active travelers are located, but it should be interpreted with population context in mind.
|
| 279 |
""")
|
| 280 |
|
| 281 |
+
elif page == "Visual 4":
|
| 282 |
+
df = pd.read_csv('dataset.csv')
|
| 283 |
+
df = df.drop(['Data Comment'], axis=1)
|
| 284 |
+
df.drop(columns=['Unnamed: 8'], inplace=True, errors='ignore')
|
| 285 |
+
df.columns = df.columns.str.strip()
|
| 286 |
+
df['Value'] = pd.to_numeric(df['Value'], errors='coerce')
|
| 287 |
+
if 'Transportation Type' in df.columns:
|
| 288 |
+
df['Transportation Type'] = df['Transportation Type'].str.replace("Transportation Type :", "").str.strip()
|
| 289 |
+
|
| 290 |
+
fips_df = pd.read_csv("https://raw.githubusercontent.com/kjhealy/fips-codes/refs/heads/master/state_and_county_fips_master.csv")
|
| 291 |
+
|
| 292 |
+
merged_df = df.merge(
|
| 293 |
+
fips_df[['fips', 'name']],
|
| 294 |
+
left_on='CountyFIPS',
|
| 295 |
+
right_on='fips',
|
| 296 |
+
how='left'
|
| 297 |
+
)
|
| 298 |
+
|
| 299 |
+
merged_df['County'] = merged_df['name']
|
| 300 |
+
merged_df.drop(columns=['fips', 'name'], inplace=True)
|
| 301 |
+
merged_df = merged_df.fillna(9999)
|
| 302 |
+
|
| 303 |
+
pop_data = pd.read_csv('pop_data.csv')
|
| 304 |
+
df_info = pop_data.groupby('CTYNAME', as_index=False).first()
|
| 305 |
+
final_merged_df = pd.merge(merged_df, df_info, left_on='County', right_on='CTYNAME', how='left')
|
| 306 |
+
final_merged_df['pop_per_unit'] = final_merged_df['POPESTIMATE2019'] / final_merged_df['Value']
|
| 307 |
+
final_merged_df['ratio'] = final_merged_df['Value']/final_merged_df['POPESTIMATE2019']
|
| 308 |
+
master_df=final_merged_df
|
| 309 |
+
|
| 310 |
+
st.sidebar.title("Year Comparison")
|
| 311 |
+
year_options = sorted(master_df['Start Year'].dropna().unique())
|
| 312 |
+
year1 = st.sidebar.selectbox("Select first year", year_options, index=year_options.index(2017))
|
| 313 |
+
year2 = st.sidebar.selectbox("Select second year", year_options, index=year_options.index(2018))
|
| 314 |
+
|
| 315 |
+
st.title("County-Level Value Change Visualization")
|
| 316 |
+
st.write(f"Showing change in total 'Value' from {year1} to {year2} by county.")
|
| 317 |
+
|
| 318 |
+
df_filtered = master_df[master_df['Start Year'].isin([year1, year2])]
|
| 319 |
+
value_by_year = df_filtered.groupby(['CountyFIPS', 'Start Year'])['Value'].sum().unstack()
|
| 320 |
+
value_by_year['Change'] = value_by_year[year2] - value_by_year[year1]
|
| 321 |
+
value_by_year = value_by_year.reset_index()
|
| 322 |
+
|
| 323 |
+
county_info = df[['CountyFIPS', 'County', 'State', 'StateFIPS']].drop_duplicates()
|
| 324 |
+
change_df = county_info.merge(value_by_year[['CountyFIPS', 'Change']], on='CountyFIPS')
|
| 325 |
+
|
| 326 |
+
counties = alt.topo_feature("https://raw.githubusercontent.com/vega/vega/refs/heads/main/docs/data/us-10m.json", "counties")
|
| 327 |
+
alt.data_transformers.disable_max_rows()
|
| 328 |
+
|
| 329 |
+
base = alt.Chart(counties).mark_geoshape(fill='lightgray').encode().project('albersUsa')
|
| 330 |
+
|
| 331 |
+
top = alt.Chart(counties).mark_geoshape().transform_lookup(
|
| 332 |
+
lookup='id',
|
| 333 |
+
from_=alt.LookupData(change_df, 'CountyFIPS', ['Change'])
|
| 334 |
+
).encode(
|
| 335 |
+
color=alt.Color('Change:Q', title='Change in Value', scale=alt.Scale(scheme='purplegreen'))
|
| 336 |
+
)
|
| 337 |
+
|
| 338 |
+
st.altair_chart(base+top, use_container_width=True)
|
| 339 |
+
|
| 340 |
+
st.markdown("""
|
| 341 |
+
This visualization was to better vizualise what is being graphed in visual 2. It better singles out which counties are going through net changes in the amount of individuals who commute to work using bicycles and by walking.
|
| 342 |
+
The net changes are calculated by subtracting the second year from the first year. This leads to a net change which is visualized by the color scale. Darker shades mean higher net values. Lighter shades mean lower net values. Positive values are shaded green and negative values are shaded purple.
|
| 343 |
+
*NOTE: counties presented in gray means that there was no datapoint for that county.
|
| 344 |
+
""")
|
| 345 |
+
|
| 346 |
# ----------------------------
|
| 347 |
# Page: Conclusion
|
| 348 |
# ----------------------------
|