在熊猫蟒蛇中获取数据框列时出错



我有这个简单的代码:

import pathlib
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
wdataset =pd.read_csv('C:MyDatasampleData.csv', header=None)
print(wdataset.tail(3))
print(wdataset[0])

当我运行此代码时,我收到此错误:

Traceback (most recent call last):
File "C:UsersjAppDataLocalProgramsPythonPython37libsite-packagespandascoreindexesbase.py", line 3078, in get_loc
return self._engine.get_loc(key)
File "pandas_libsindex.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
File "pandas_libsindex.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
File "pandas_libshashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas_libshashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 0
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:Usersj.vscodeextensionsms-python.python-2019.8.29288pythonFilesptvsd_launcher.py", line 43, in <module>
main(ptvsdArgs)
File "c:Usersj.vscodeextensionsms-python.python-2019.8.29288pythonFileslibpythonptvsd__main__.py", line 432, in main
run()
File "c:Usersj.vscodeextensionsms-python.python-2019.8.29288pythonFileslibpythonptvsd__main__.py", line 316, in run_file
runpy.run_path(target, run_name='__main__')
File "C:UsersjAppDataLocalProgramsPythonPython37librunpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
File "C:UsersjAppDataLocalProgramsPythonPython37librunpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "C:UsersjAppDataLocalProgramsPythonPython37librunpy.py", line 85, in _run_code
exec(code, run_globals)
File "c:MyDataRCSV.py", line 12, in <module>
print(wdataset[0])
File "C:UsersjAppDataLocalProgramsPythonPython37libsite-packagespandascoreframe.py", line 2688, in __getitem__
return self._getitem_column(key)
File "C:UsersjAppDataLocalProgramsPythonPython37libsite-packagespandascoreframe.py", line 2695, in _getitem_column
return self._get_item_cache(key)
File "C:UsersjAppDataLocalProgramsPythonPython37libsite-packagespandascoregeneric.py", line 2489, in _get_item_cache
values = self._data.get(item)
File "C:UsersjAppDataLocalProgramsPythonPython37libsite-packagespandascoreinternals.py", line 4115, in get
loc = self.items.get_loc(item)
File "C:UsersjAppDataLocalProgramsPythonPython37libsite-packagespandascoreindexesbase.py", line 3080, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas_libsindex.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
File "pandas_libsindex.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
File "pandas_libshashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas_libshashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 0

问题是什么,我该如何解决?

您可以使用 iloc 或 loc 命令。使用 iloc,您可以指定数字,而使用 loc 可以指定名称。例如

data.iloc[:,0] # to get all rows of zeroth column
#For more than one column
data.iloc[:,0:3]

data.loc[:,['zeroth column name']] # get all rows of zeroth column
#For more than one column
data.loc[:,['first column name','second column name']]

最新更新