Error : UndefinedVariableError: name 'injured_persons' is not defined

1.1k views Asked by At

I am learning streamlit and when I was going through a tutorial video ,where access to remote desktop was already provided, which has all the prerequisite (Atom as text editor & streamlit ) the following code worked well there and gave the required output, However when I tried the same code in my system(Sublime as text editor & anaconda terminal to run streamlit) from Sublime text I got the error "UndefinedVariableError: name 'injured_persons' is not defined"

CODE :

import streamlit as st
import pandas as pd
import numpy as mp

DATA_URL=("E://Saumya//STUDY//DS//Motor_Vehicle_Collisions_-_Crashes.csv")

st.title("Motor Vehicle Collsion in New york City")
st.markdown("This application is to analyze motor vehicle collision in NYC")

@st.cache(persist=True)
def load_data(nrows):
    data=pd.read_csv(DATA_URL,nrows=nrows,parse_dates=[['CRASH DATE','CRASH TIME']])
    data.dropna(subset=['LATITUDE', 'LONGITUDE'], inplace=True)
    lowercase=lambda x : str(x).lower()
    data.rename(lowercase,axis='columns',inplace=True)
    data.rename(columns={'crash_date_crash_time' : 'date/time'},inplace=True)
    return data

data=load_data(100000)
    
st.header("How many people are injured")
injured_people = st.slider("Number of people injured", 0, 19)
st.map(data.query("injured_persons>= @injured_people")[["latitude", "longitude"]].dropna(how="any"))


if st.checkbox("Show Raw data",False):
    st.subheader("Raw Data")
    st.write(data)

Below is the link to my error screenshot

1

There are 1 answers

0
Ajay Verma On BEST ANSWER

Most probably it's happening because 'injured_persons' is not in your data.

for example:

df = pd.DataFrame({'A': range(1, 6),
                   'B': range(10, 0, -2),
                   'C': range(10, 5, -1)})

if I try to access a column name D which is not in my DataFrame

df.query('D > B')

it throws me an error

UndefinedVariableError: name 'D' is not defined

Please check if you data has 'injured_persons'. you can do so by print('injured_persons' in data.columns)