gpt4 book ai didi

python - 读取DataFrame的列失败

转载 作者:太空宇宙 更新时间:2023-11-03 15:56:47 24 4
gpt4 key购买 nike

我正在使用 python 2.7 和 Anaconda2。当我将 txt 文件简单读取到数据帧中时 (df = pd.read_table("/home/testtab.txt",sep='\t',index_col=False)),然后读取其中一列如下 (df["col1"].head()),它给了我一个错误(见下文)。此外,行索引也存在,而我希望使用 index_col=False 禁用它。df.columns 的输出如下:

Index([u'col1', u'col2'],dtype='object')

错误:

KeyError                                  Traceback (most recent call last)
<ipython-input-3-c472f01c3482> in <module>()
----> 1 df["col1"].head()

/home/gooo/anaconda2/lib/python2.7/site-packages/pandas/core/frame.py in __getitem__(self, key)
1995 return self._getitem_multilevel(key)
1996 else:
-> 1997 return self._getitem_column(key)
1998
1999 def _getitem_column(self, key):

/home/gooo/anaconda2/lib/python2.7/site-packages/pandas/core/frame.py in _getitem_column(self, key)
2002 # get column
2003 if self.columns.is_unique:
-> 2004 return self._get_item_cache(key)
2005
2006 # duplicate columns & possible reduce dimensionality

/home/gooo/anaconda2/lib/python2.7/site-packages/pandas/core/generic.py in _get_item_cache(self, item)
1348 res = cache.get(item)
1349 if res is None:
-> 1350 values = self._data.get(item)
1351 res = self._box_item_values(item, values)
1352 cache[item] = res

/home/gooo/anaconda2/lib/python2.7/site-packages/pandas/core/internals.py in get(self, item, fastpath)
3288
3289 if not isnull(item):
-> 3290 loc = self.items.get_loc(item)
3291 else:
3292 indexer = np.arange(len(self.items))[isnull(self.items)]

/home/gooo/anaconda2/lib/python2.7/site-packages/pandas/indexes/base.py in get_loc(self, key, method, tolerance)
1945 return self._engine.get_loc(key)
1946 except KeyError:
-> 1947 return self._engine.get_loc(self._maybe_cast_indexer(key))
1948
1949 indexer = self.get_indexer([key], method=method, tolerance=tolerance)

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4154)()

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4018)()

pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12368)()

pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12322)()

KeyError: 'col1'

编辑:

df.info() 的输出:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 7499900 entries, 0 to 7499899
Data columns (total 18 columns):
col1 object
col2 float64
dtypes: float64(1), object(1)
memory usage: 1.0+ GB

最佳答案

pandas 版本 0.18.1 出现问题,升级到 0.19.1 后工作正常。

我认为可能是一些错误。

关于python - 读取DataFrame的列失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40709567/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com