KeyError: df check code error before uploading data

Kindly need your support as i’m getting error after run code even i check if df is not None before run the code , but error dis appear after upload data

def loadData():
    data =pd.DataFrame()
    uploadFile= st.file_uploader("Choose output.csv file")
    if uploadFile is not None:
       df = pd.read_csv(uploadFile)
       data = df
    return data

df = loadData() 

if df is not None:
   def mergePop():
       
          #appended pop list
          popList=[]
          #get US from df and get first 5 No to irritae
          #usList = df['UserName'].to_list()  
          
          for us in df['UserName'] :
              #us2=str(us)
              #st.write(type(us2))
              if us is not None:
                 if us[:2] == '02':
                  popList.append('Cairo')
              
                 elif us[:2] == '03':
                  popList.append('Alx')
                  
                 else:
                  popList.append('NA')
                  
          popS= pd.Series(popList)      
          return popS
          
if df is not None:
   df['pop']=  pd.Series(mergePop())
   st.dataframe(df)

----------------------
KeyError: 'UserName'

Traceback:

File "c:\anaconda3\envs\elbouhy-env\lib\site-packages\streamlit\script_runner.py", line 332, in _run_script
    exec(code, module.__dict__)File "C:\Users\ramye\Desktop\test3.py", line 46, in <module>
    df['pop']=  pd.Series(mergePop())File "C:\Users\ramye\Desktop\test3.py", line 28, in mergePop
    for us in df['UserName'] :File "c:\anaconda3\envs\elbouhy-env\lib\site-packages\pandas\core\frame.py", line 3024, in __getitem__
    indexer = self.columns.get_loc(key)File "c:\anaconda3\envs\elbouhy-env\lib\site-packages\pandas\core\indexes\base.py", line 3082, in get_loc
    raise KeyError(key) from err

image

Hi @ramyelbouhy, welcome to the Streamlit community!

Key Errors mean that the column you are trying to access doesn’t exist. The easiest way to debug this is to place a st.write(df.columns) in your code right before the for loop, to see what the values are actually named. Then you can either change the name of the column you are trying to reference, or possibly the column names aren’t coming through correctly on your pd.read_csv statement.

Best,
Randy

2 Likes

This annoying error means that Pandas can not find your column name in your dataframe. Before doing anything with the data frame, use print(df.columns) to see dataframe column exist or not.

print(df.columns)

I was getting a similar kind of error in one of my codes. Turns out, that particular index was missing from my data frame as I had dropped the empty dataframe 2 rows. If this is the case, you can do df.reset_index(inplace=True) and the error should be resolved.