繁体   English   中英

使用 python 计算 excel 中的单元格时出现错误,但我不知道为什么?

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

这是代码:

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") 

这是 excel 文件Excel 文件

我收到这个错误

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'

为什么?这个错误。我不明白为什么会出现错误?

如前所述,有一个 KeyError: 'leta'。 确保您正确编写了列名称。

  • 检查列名中是否有空格

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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