Red*_*ven 4 python group-by pandas
在熊猫队,我正在做一个看似简单的小组.该列是一个字符串列,没有NaN或奇怪的字符串.但是,我一直收到以下错误.有谁知道为什么会发生这种情况?我觉得它可能与我的数据有关,但一切似乎都没问题......
我在跑步 by_user = df.groupby('User')
和堆栈跟踪:
by_user = df.groupby('User')
File "c:\Anaconda\lib\site-packages\pandas\core\generic.py", line 2773, in groupby
sort=sort, group_keys=group_keys, squeeze=squeeze)
File "c:\Anaconda\lib\site-packages\pandas\core\groupby.py", line 1142, in groupby
return klass(obj, by, **kwds)
File "c:\Anaconda\lib\site-packages\pandas\core\groupby.py", line 388, in __init__ level=level, sort=sort)
File "c:\Anaconda\lib\site-packages\pandas\core\groupby.py", line 2041, in _get_grouper
gpr = obj[gpr]
File "c:\Anaconda\lib\site-packages\pandas\core\frame.py", line 1678, in __getitem__
return self._getitem_column(key)
File "c:\Anaconda\lib\site-packages\pandas\core\frame.py", line 1685, in _get item_column
return self._get_item_cache(key)
File "c:\Anaconda\lib\site-packages\pandas\core\generic.py", line 1052, in _ge
t_item_cache
values = self._data.get(item)
File "c:\Anaconda\lib\site-packages\pandas\core\internals.py", line 2565, in get
loc = self.items.get_loc(item)
File "c:\Anaconda\lib\site-packages\pandas\core\index.py", line 1181, in get_loc
return self._engine.get_loc(_values_from_object(key))
File "index.pyx", line 129, in pandas.index.IndexEngine.get_loc (pandas\index.
c:3656)
File "index.pyx", line 149, in pandas.index.IndexEngine.get_loc (pandas\index.
c:3534)
File "hashtable.pyx", line 696, in pandas.hashtable.PyObjectHashTable.get_item
(pandas\hashtable.c:11911)
File "hashtable.pyx", line 704, in pandas.hashtable.PyObjectHashTable.get_item
(pandas\hashtable.c:11864)
KeyError: 'User'
Run Code Online (Sandbox Code Playgroud)
df.info():
User Code 175167 non-null object
Version 175167 non-null object
Date Accessed 175167 non-null datetime64[ns]
Series 175167 non-null object
Software 175167 non-null object
User 175167 non-null object
Run Code Online (Sandbox Code Playgroud)
[从评论中移出]
很容易错过列名中的尾随空格,但您可以df.columns手动检查:
>>> df = pd.DataFrame({"User": [1,2]})
>>> df2 = pd.DataFrame({"User ": [1,2]})
>>> df
User
0 1
1 2
>>> df2
User
0 1
1 2
>>> df.columns
Index([u'User'], dtype='object')
>>> df2.columns
Index([u'User '], dtype='object')
Run Code Online (Sandbox Code Playgroud)
(为了稍稍剥离窗帘,我怀疑这样的事情可能正在发生,因为当我模拟我自己的DataFrame并查看时df.info(),我没有看到列名和数字之间的空间,因为你的输出似乎节目.)
| 归档时间: |
|
| 查看次数: |
13352 次 |
| 最近记录: |