python - 在熊猫python中获取数据框的一列时出错
问题描述
我有这个简单的代码:
import pathlib
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
wdataset =pd.read_csv('C:\MyData\sampleData.csv', header=None)
print(wdataset.tail(3))
print(wdataset[0])
当我运行此代码时,我收到此错误:
Traceback (most recent call last):
File "C:\Users\j\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\indexes\base.py", line 3078, in get_loc
return self._engine.get_loc(key)
File "pandas\_libs\index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_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:\Users\j\.vscode\extensions\ms-python.python-2019.8.29288\pythonFiles\ptvsd_launcher.py", line 43, in <module>
main(ptvsdArgs)
File "c:\Users\j\.vscode\extensions\ms-python.python-2019.8.29288\pythonFiles\lib\python\ptvsd\__main__.py", line 432, in main
run()
File "c:\Users\j\.vscode\extensions\ms-python.python-2019.8.29288\pythonFiles\lib\python\ptvsd\__main__.py", line 316, in run_file
runpy.run_path(target, run_name='__main__')
File "C:\Users\j\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
File "C:\Users\j\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "C:\Users\j\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "c:\MyData\RCSV.py", line 12, in <module>
print(wdataset[0])
File "C:\Users\j\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\frame.py", line 2688, in __getitem__
return self._getitem_column(key)
File "C:\Users\j\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\frame.py", line 2695, in _getitem_column
return self._get_item_cache(key)
File "C:\Users\j\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\generic.py", line 2489, in _get_item_cache
values = self._data.get(item)
File "C:\Users\j\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\internals.py", line 4115, in get
loc = self.items.get_loc(item)
File "C:\Users\j\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\indexes\base.py", line 3080, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas\_libs\index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_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']]
推荐阅读
- matlab - histfit 似乎正在修改图形数据
- r - 如何使用`R``cem`包估计CEM匹配数据的线性回归模型的系数?
- python - 这个平台缺少一个正常运行的 sem_open 实现,因此,需要所需的同步原语
- django - 如何根据 django 中的特定 id 创建单独的页面?
- visual-studio-code - 直接在远程ssh中启动vscode
- jenkins - 詹金斯:如果作业名称包含删除作业
- ios - 如何使用一个动画动画删除所有子 UIViewControllers?
- haskell - 为什么odd.fst 不能与过滤器功能一起使用?
- math - 椭圆/椭圆和基于和弦的数学
- typescript - 位置重新加载使 webpack 开发服务器崩溃 - Vue 2