【问题标题】:KeyError while tring to write an additional field in Python Pandas dataframe在 Python Pandas 数据框中写入附加字段时出现 KeyError
【发布时间】:2020-11-26 03:42:07
【问题描述】:

我想在数据框positions_deposits 中添加一个计算字段“分数”。

当我对 pandas 数据框 positions_deposits 运行以下操作时,

for i in range(len(positions_deposits)):
    <Read some values from the dataframe which would be passed to a function in the next line>
    Score = RAG_function (Amber_threshold, Red_threshold, Type_threshold, Values)
    positions_deposits['Score'].loc[i] = Score

我收到以下错误。您能否指导我解决我所犯的错误以及如何解决它?

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~/.local/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2894             try:
-> 2895                 return self._engine.get_loc(casted_key)
   2896             except KeyError as err:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'Score'

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

KeyError                                  Traceback (most recent call last)
<ipython-input-201-7d0481b84aa4> in <module>
      6     Values = positions_deposits['Values'].loc[i]
      7 #     Score = RAG_function (Amber_threshold, Red_threshold, Type_threshold, Values)
----> 8     positions_deposits["Score"].loc[i] = RAG_function (Amber_threshold, Red_threshold, Type_threshold, Values)
      9 
     10 #     print("Score is %i.00" %Score)

~/.local/lib/python3.8/site-packages/pandas/core/frame.py in __getitem__(self, key)
   2904             if self.columns.nlevels > 1:
   2905                 return self._getitem_multilevel(key)
-> 2906             indexer = self.columns.get_loc(key)
   2907             if is_integer(indexer):
   2908                 indexer = [indexer]

~/.local/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2895                 return self._engine.get_loc(casted_key)
   2896             except KeyError as err:
-> 2897                 raise KeyError(key) from err
   2898 
   2899         if tolerance is not None:

KeyError: 'Score'

请注意:如果我print(Score),则没有错误。这意味着函数 RAG_function 正在执行但数据帧失败。

谢谢!

【问题讨论】:

  • 查看 Traceback 显示的位置,positions_deposits["Score"].loc[i],表示您的字典没有“Score”键。
  • 我在循环之前用Score = 999 启动了Score 变量。错误消失了,我收到了SettingWithCopyWarning: 警告。看来,Python 警告我数据框正在覆盖该值。

标签: python pandas keyerror


【解决方案1】:

您可能想了解.loc.iloc 的工作原理。不过话说回来,还有一种更好的方法:

import pandas
import random

df = pandas.DataFrame([{"A": random.randint(0,100), "B": random.randint(0,100)} for _ in range(100)])

def rag_function(row):
    A = row["A"]
    B = row["B"]
    return A * B

df["Score"] = df.apply(rag_function, axis=1)

注意:我没有你的RAG_function,所以我创建了一些随机函数。想法是将此函数应用于数据框中的每个 row

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-07
    • 2021-05-11
    • 1970-01-01
    相关资源
    最近更新 更多