Python pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: 0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47603430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:17:08  来源:igfitidea点击:

pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: 0

pythonpandascsv

提问by Mona Jalal

I am trying to map emotions from one dataset to another and drop everything that is bigger than 6 in the current dataset. How should I fix this error?

我试图将情绪从一个数据集映射到另一个数据集,并删除当前数据集中大于 6 的所有内容。我应该如何修复这个错误?

import pandas as pd

csv_file = 'sample.csv'
count = 1
my_filtered_csv = pd.read_csv(csv_file, usecols=['subDirectory_filePath', 'expression'])
#my_filtered_csv['expression'] = my_filtered_csv['expression'].map({ '0':'6', '1':'3', '2':'4', '3':'5', '4':'2', '5':'1', '6':'0'})

df = pd.DataFrame(columns=['subDirectory_filePath', 'expression'])
print(my_filtered_csv.dtypes.index)
filtered_csv = my_filtered_csv[my_filtered_csv.expression <= 6 ]
for i in range(len(filtered_csv['expression'])):
    if filtered_csv['expression'][i]==0:
        filtered_csv['expression'][i] = 6
    elif filtered_csv['expression'][i]==1:
        filtered_csv['expression'][i] = 3
    elif filtered_csv['expression'][i]==2:
        filtered_csv['expression'][i] = 4
    elif filtered_csv['expression'][i]==3:
        filtered_csv['expression'][i] = 5
    elif filtered_csv['expression'][i]==4:
        filtered_csv['expression'][i] = 2
    elif filtered_csv['expression'][i]==5:
        filtered_csv['expression'][i] = 1
    elif filtered_csv['expression'][i]==6:
        filtered_csv['expression'][i] = 0

print(len(my_filtered_csv))
print('****')

for val in df['expression']:
    print(val)
emotion_map = { '0':'6', '1':'3', '2':'4', '3':'5', '4':'2', '5':'1', '6':'0'}
print(emotion_map)

for key, value in emotion_map.items():
    print(key,' : ', value)


   '''
affectnet

0: Neutral,
1: Happiness,
2: Sadness,
3: Surprise,
4: Fear,
5: Disgust,
6: Anger,
7: Contempt,
8: None,
9: Uncertain,
10: No-Face

FER13
 (0=Angry, 1=Disgust, 2=Fear, 3=Happy, 4=Sad, 5=Surprise, 6=Neutral).
 0-->6
 1-->3
 2-->4
 3-->5
 4-->2
 5-->1
 6-->0

'''

Error is:

错误是:

Index(['subDirectory_filePath', 'expression'], dtype='object')
Traceback (most recent call last):
  File "/Users/mona/anaconda/lib/python3.6/site-packages/pandas/core/series.py", line 778, in _set_with_engine
    self.index._engine.set_value(values, key, value)
  File "pandas/_libs/index.pyx", line 116, in pandas._libs.index.IndexEngine.set_value
  File "pandas/_libs/index.pyx", line 124, in pandas._libs.index.IndexEngine.set_value
  File "pandas/_libs/index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1210, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1218, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 0

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/mona/anaconda/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 2442, in get_loc
    return self._engine.get_loc(key)
  File "pandas/_libs/index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1210, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1218, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 0

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/mona/anaconda/lib/python3.6/site-packages/pandas/core/series.py", line 719, in setitem
    self._set_with_engine(key, value)
  File "/Users/mona/anaconda/lib/python3.6/site-packages/pandas/core/series.py", line 781, in _set_with_engine
    values[self.index.get_loc(key)] = value
  File "/Users/mona/anaconda/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 2444, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas/_libs/index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1210, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1218, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 0

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/mona/CS585/project/affnet/emotion_map.py", line 17, in <module>
    df['expression'][i] = 3
  File "/Users/mona/anaconda/lib/python3.6/site-packages/pandas/core/series.py", line 771, in __setitem__
    setitem(key, value)
  File "/Users/mona/anaconda/lib/python3.6/site-packages/pandas/core/series.py", line 728, in setitem
    values[key] = value
IndexError: index 0 is out of bounds for axis 0 with size 0

Process finished with exit code 1

A few lines of the cvs is:

cvs的几行是:

,subDirectory_filePath,expression
0,689/737db2483489148d783ef278f43f486c0a97e140fc4b6b61b84363ca.jpg,1
1,392/c4db2f9b7e4b422d14b6e038f0cdc3ecee239b55326e9181ee4520f9.jpg,0
2,468/21772b68dc8c2a11678c8739eca33adb6ccc658600e4da2224080603.jpg,0
3,944/06e9ae8d3b240eb68fa60534783eacafce2def60a86042f9b7d59544.jpg,1
4,993/02e06ee5521958b4042dd73abb444220609d96f57b1689abbe87c024.jpg,8

回答by Peter Leimbigler

I think this error comes from your [i]notation, which is trying to look for the DataFrame index value of 0, which doesn't exist. Try replacing every instance of [i]with .iloc[i].

我认为此错误来自您的[i]符号,它试图查找不存在的 DataFrame 索引值 0。尝试更换的每个实例[i].iloc[i]

Also, you should be able to replace the for loop with much more compact, readable, and less error-prone code, especially since you define emotion_mapbut use it only for output. Try changing the mapping from strings to integers with emotion_map = { 0:6, 1:3, 2:4, 3:5, 4:2, 5:1, 6:0}, then move it to just under filtered_csv = ..., and replace that forloop with

此外,您应该能够用更紧凑、可读且不易出错的代码替换 for 循环,尤其是因为您定义了emotion_map它但仅将其用于输出。尝试使用 将映射从字符串更改为整数emotion_map = { 0:6, 1:3, 2:4, 3:5, 4:2, 5:1, 6:0},然后将其移至正下方filtered_csv = ...,并用 替换该for循环

filtered_csv['expression'] = filtered_csv['expression'].replace(emotion_map)

回答by Abhishek Jain

Pandas Dataframe should always be used with iloc as it does not support to indexing with 0. I faced the same issue when there was only single element & I was trying to access that single element by prefixing 0......

Pandas Dataframe 应该始终与 iloc 一起使用,因为它不支持使用 0 进行索引。当只有单个元素时我遇到了同样的问题,我试图通过添加 0 前缀来访问该单个元素......

回答by Joseph

Easy thing to do is also to convert your dataframeto an numpyarray

简单的事情也是将您的转换dataframenumpy数组

df=np.array(df)

df=np.array(df)

then you can use your for loopwithout any problems

然后你可以for loop毫无问题地使用你的