错误:pandas 哈希表 keyerror

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43736163/
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-09-14 03:31:32  来源:igfitidea点击:

Error: pandas hashtable keyerror

pythonpandasdataframe

提问by Abhinav Karthick

I have successfully read a csv file using pandas. When I am trying to print the a particular column from the data frame i am getting keyerror. Hereby i am sharing the code with the error.

我已经使用Pandas成功读取了一个 csv 文件。当我尝试从数据框中打印特定列时,我收到了 keyerror。在此,我与错误共享代码。

import pandas as pd
reviews_new = pd.read_csv("D:\aviva.csv")
reviews_new['review']

**

**

reviews_new['review']
Traceback (most recent call last):
  File "<ipython-input-43-ed485b439a1c>", line 1, in <module>
    reviews_new['review']
  File "C:\Users216\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\core\frame.py", line 1997, in __getitem__
    return self._getitem_column(key)
  File "C:\Users216\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\core\frame.py", line 2004, in _getitem_column
    return self._get_item_cache(key)
  File "C:\Users216\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\core\generic.py", line 1350, in _get_item_cache
    values = self._data.get(item)
  File "C:\Users216\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\core\internals.py", line 3290, in get
    loc = self.items.get_loc(item)
  File "C:\Users216\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\indexes\base.py", line 1947, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas\index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas\index.c:4154)
  File "pandas\index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas\index.c:4018)
  File "pandas\hashtable.pyx", line 675, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12368)
  File "pandas\hashtable.pyx", line 683, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12322)
KeyError: 'review'

**

**

Can someone help me in this ?

有人可以帮助我吗?

回答by jezrael

I think first is best investigate, what are real columns names, if convert to list better are seen some whitespaces or similar:

我认为首先最好调查一下,什么是真正的列名,如果更好地转换为列表会看到一些空格或类似的:

print (reviews_new.columns.tolist())


I think there can be 2 problems (obviously):

我认为可能有两个问题(显然):

1.whitespaces in columns names (maybe in data also)

1. 列名中的空格(也可能在数据中)

Solutions are stripwhitespaces in column names:

解决方案是strip列名中的空格:

reviews_new.columns = reviews_new.columns.str.strip()

Or add parameter skipinitialspaceto read_csv:

或添加参数skipinitialspaceread_csv

reviews_new = pd.read_csv("D:\aviva.csv", skipinitialspace=True)

2.different separator as default ,

2.默认分隔符不同 ,

Solution is add parameter sep:

解决方案是添加参数sep

#sep is ;
reviews_new = pd.read_csv("D:\aviva.csv", sep=';')
#sep is whitespace
reviews_new = pd.read_csv("D:\aviva.csv", sep='\s+')
reviews_new = pd.read_csv("D:\aviva.csv", delim_whitespace=True)

EDIT:

编辑:

You get whitespace in column name, so need 1.solutions:

您在列名中获得空格,因此需要1.solutions

print (reviews_new.columns.tolist())
['Name', ' Date', ' review'] 
          ^        ^

回答by svp

import pandas as pd
df=pd.read_csv("file.txt", skipinitialspace=True)
df.head()
df['review']