尝试从数据框获取值时出错

时间:2020-10-11 04:46:10

标签: python

我正在尝试从数据框中的单元格获取值。附件是我的代码:

Ftr=f['Word'].iloc[k]
# print((Ftr))
if Ftr in list1:
    X=f['Token'].iloc[k]

但是当我执行它时,对于几个字符串,我得到以下错误:

Traceback (most recent call last):

  File "/Users/shells/opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 2646, in get_loc
    return self._engine.get_loc(key)

  File "pandas/_libs/index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc

  File "pandas/_libs/index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc

  File "pandas/_libs/hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item

  File "pandas/_libs/hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item

KeyError: 'miscforsale'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "<ipython-input-147-a82083f97449>", line 1, in <module>
    PA_posterior['miscforsale'][0]

  File "/Users/shells/opt/anaconda3/lib/python3.8/site-packages/pandas/core/frame.py", line 2800, in __getitem__
    indexer = self.columns.get_loc(key)

  File "/Users/shells/opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 2648, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))

  File "pandas/_libs/index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc

  File "pandas/_libs/index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc

  File "pandas/_libs/hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item

  File "pandas/_libs/hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item

KeyError: 'miscforsale'

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

对不起,没有足够的声誉来发表评论。

您的DataFrame最有可能不包含名称为“ miscforsale”的列。

请记住,每次收到“ KeyError”错误时,都应该查看访问DataFrame列的代码,并查看名称是否实际匹配。

由于您尚未说明DataFrame结构,因此我不能给您太多帮助。