【发布时间】:2018-08-09 04:57:54
【问题描述】:
我正在编写代码以使用pandas 读取csv 文件,我看到了该包的一些奇怪功能。我的文件有我想忽略的列名,所以我使用header = 0 或'infer' 而不是None。但我看到了一些奇怪的东西。
当我使用None 并且想要获取特定列时,我只需要执行df[column_index],但是当我使用0 或'infer' 时,我需要执行df.ix[:,column_index] 来获取列,否则, 对于df[column_index] 我收到以下错误:
Traceback(最近一次调用最后一次):文件 “/home/sarvagya/anaconda3/envs/tf/lib/python3.6/site-packages/pandas/core/indexes/base.py”, 第 2525 行,在 get_loc 中 return self._engine.get_loc(key) 文件“pandas/_libs/index.pyx”,第 117 行,在 pandas._libs.index.IndexEngine.get_loc 文件中 “pandas/_libs/index.pyx”,第 139 行,在 pandas._libs.index.IndexEngine.get_loc 文件 “pandas/_libs/hashtable_class_helper.pxi”,第 1265 行,在 pandas._libs.hashtable.PyObjectHashTable.get_item 文件 “pandas/_libs/hashtable_class_helper.pxi”,第 1273 行,在 pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: column_index
在处理上述异常的过程中,又发生了一个异常:
Traceback(最近一次调用最后一次):文件“”,第 1 行,in 文件 “/home/sarvagya/anaconda3/envs/tf/lib/python3.6/site-packages/pandas/core/frame.py”, 第 2139 行,在 getitem 中 返回 self._getitem_column(key) 文件“/home/sarvagya/anaconda3/envs/tf/lib/python3.6/site-packages/pandas/core/frame.py”, 第 2146 行,在 _getitem_column 返回 self._get_item_cache(key) 文件“/home/sarvagya/anaconda3/envs/tf/lib/python3.6/site-packages/pandas/core/generic.py”, 第 1842 行,在 _get_item_cache 中 值 = self._data.get(item) 文件“/home/sarvagya/anaconda3/envs/tf/lib/python3.6/site-packages/pandas/core/internals.py”, 第 3843 行,在获取 loc = self.items.get_loc(item) 文件“/home/sarvagya/anaconda3/envs/tf/lib/python3.6/site-packages/pandas/core/indexes/base.py”, 第 2527 行,在 get_loc 中 return self._engine.get_loc(self._maybe_cast_indexer(key)) 文件“pandas/_libs/index.pyx”,第 117 行,在 pandas._libs.index.IndexEngine.get_loc 文件 “pandas/_libs/index.pyx”,第 139 行,在 pandas._libs.index.IndexEngine.get_loc 文件 “pandas/_libs/hashtable_class_helper.pxi”,第 1265 行,在 pandas._libs.hashtable.PyObjectHashTable.get_item 文件 “pandas/_libs/hashtable_class_helper.pxi”,第 1273 行,在 pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: column_index
有人可以帮忙吗?为什么会这样?
【问题讨论】:
标签: python-3.x pandas csv dataframe