簡體   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