简体   繁体   中英

I am getting error for calculating cells in excel using python but i dont know why?

This is the code:

import pandas as pd
file_name = "C:/Users/jains/Downloads/Python/Python/SA.xlsx"
df = pd.read_excel(file_name) 

df['Sum'] = df['data']*df['petha']+df['leta']*df['deta']


df.to_excel("C:/Users/jains/Downloads/Python/Python/SA.xlsx") 

this is excel file Excel File

I am getting this error

Traceback (most recent call last):
  File "C:\Users\jains\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\base.py", line 3361, in get_loc
    return self._engine.get_loc(casted_key)
  File "pandas\_libs\index.pyx", line 76, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 5198, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 5206, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'leta'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "c:/Users/jains/Downloads/Python/Python/SCreatefile2.py", line 5, in <module>
    df['Sum'] = df['data']*df['petha']*df['leta']*df['deta']
  File "C:\Users\jains\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\frame.py", line 3455, in __getitem__
    indexer = self.columns.get_loc(key)
  File "C:\Users\jains\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\base.py", line 3363, in get_loc
    raise KeyError(key) from err
KeyError: 'leta'

why?this error.I cant understand why the error is coming?

As noted there is a KeyError: 'leta'. Make sure you are writing the column name correctly.

  • check if there is a space in the column name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM