我正在python中执行以下操作:
data = pd.read_csv('data/adult.csv', ',', header=None)
print(data["income"].value_counts())
我不断收到以下错误:
Traceback (most recent call last):
File "/usr/localnfs/Compiler/python/Anaconda3-2019.07/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 2657, in get_loc
return self._engine.get_loc(key)
File "pandas/_libs/index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 129, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index_class_helper.pxi", line 91, in pandas._libs.index.Int64Engine._check_type
KeyError: 'income'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "main.py", line 42, in <module>
print(data["income"].value_counts())
File "/usr/localnfs/Compiler/python/Anaconda3-2019.07/lib/python3.7/site-packages/pandas/core/frame.py", line 2927, in __getitem__
indexer = self.columns.get_loc(key)
File "/usr/localnfs/Compiler/python/Anaconda3-2019.07/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 2659, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas/_libs/index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 129, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index_class_helper.pxi", line 91, in pandas._libs.index.Int64Engine._check_type
KeyError: 'income'
我不知道此错误来自何处。我的程序只有这两行(当然还有库)。请帮助。
如果您要使用收入作为列标题/名称,则删除header = none参数
data = pd.read_csv('data/adult.csv', ',')
print(data["income"].value_counts())
或根据您的数据框使用适当的列名,
data = pd.read_csv('data/adult.csv', ',', header=None)
print(data[14].value_counts())