Skip to content

Commit e3f8dd8

Browse files
committed
Added a dictionary as a switch to select the injured type in order no to hard code the selecbox variable and eliminate the if/else
1 parent e64a1f7 commit e3f8dd8

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

app.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
DATA_URL = (
9-
"C:/Users/Atharva/Desktop/Project/project 2/streamlit-nyc/Motor_Vehicle_Collisions_-_Crashes.csv"
9+
"Motor_Vehicle_Collisions_-_Crashes.csv"
1010
)
1111

1212
st.title("Motor Vehicle Collisions in New York City")
@@ -70,15 +70,20 @@ def load_data(nrows):
7070
st.header("Top 5 dangerous streets by affected type")
7171
select=st.selectbox('Affected type of people', ['Pedestrians', 'Cyclists', 'Motorists'])
7272

73-
if select == 'Pedestrains':
74-
st.write(original_data.query("injured_pedstrains >=1")[["on_street_name", "injured_pedstrains"]].sort_values(by=['injured_pedstrains'], ascending=False).dropna(how='any')[:5])
75-
76-
elif select == 'Cyclists':
77-
st.write(original_data.query("injured_cyclists >=1")[["on_street_name", "injured_cyclists"]].sort_values(by=['injured_cyclistss'], ascending=False).dropna(how='any')[:5])
78-
79-
else:
80-
st.write(original_data.query("injured_motorists >=1")[["on_street_name", "injured_motorists"]].sort_values(by=['injured_motorists'], ascending=False).dropna(how='any')[:5])
81-
73+
selectbox_dict = {
74+
'Pedestrians': 'injured_pedestrians',
75+
'Cyclists': 'injured_cyclists',
76+
'Motorists': 'injured_motorists'
77+
}
78+
79+
injured_type = selectbox_dict[select]
80+
st.write(
81+
original_data.query(f"{injured_type} >=1")[
82+
["on_street_name", injured_type]
83+
]
84+
.sort_values(by=[injured_type], ascending=False)
85+
.dropna(how="any")[:5]
86+
)
8287

8388
if st.checkbox("Show Raw Data", False):
8489
st.subheader('Raw Data')

0 commit comments

Comments
 (0)