'Streamlit Panda Query Function Syntax Error When Finding Column in CSV Dataframe

When Using Streamlit to build a data interface getting a syntax error. My downloaded csv dataframe has a column 'NUMBER OF PERSONS INJURED', after converting it into a dataframe with panda and trying to use the query function to reference it I'm getting errors like below. I converted the text to lower case in the dataframe. I've attached the error message and sample csv file screenshot. Github has code and sample csv file. My questions are:

1. how to fix this error? 2. What's the underlying cause of it?

Code in Question:

injured_people = st.slider("", 0, 19)
st.map(data.query("number of persons injured > @injured_people")[["latitude", "longitude"]].dropna(how="any"))

Error Message

csv sample shot of number of persons injured

Things I've tried:

  • adding '' to number of persons injured to convert to string. But then get error about st.slider being int and unable to operate with > between str & int.

  • hacking the csv by converting number of persons injured with underscore number_of_persons_injured but that throws undefined error.

  • Converting @injured_people to a string. Yes stupid I know. String() undefined error.

    injured_people = st.slider("", 0, 19)

    injured_people = string(injured_people)

Git File: https://github.com/petersun825/Bike_Crash_Dashboard_NYC/blob/master/app.py



Solution 1:[1]

pandas does not know what you mean for the column name with spaces, which leads it to try and find a column number. You can escape the spaces with backticks:

st.map(data.query("`number of persons injured` > @injured_people")[["latitude", "longitude"]].dropna(how="any"))

Solution 2:[2]

You can try by edit the column name to something simpler, like injured_person. Then restart your device and try run the streamlit again

Solution 3:[3]

st.map(data.query("injured_persons >= @injured_people")[["latitude", "longitude"]].dropna(how="any"))

This might help you, change the number_of_persons_injured to injured_persons in CSV and Your Python Code

Else try with changing NUMBER OF PERSONS INJURED in CSV instead of spaces between add underscore that might work number_of_persons_injured

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Randy Zwitch
Solution 2 Vinotaz
Solution 3 Emi OB