Dataframe操作时出现 KeyError: '列名xxx'

python确实很用来很爽很苏服,代码不多

各种库出于易用目的,做了很多默认设置,但要是不认真看API,那就会掉到坑里了。

df1.groupby(['Dn','UserLabel','BeginTime']).first()

df1['city']=df1['UserLabel'].str.slice(0,2)

出现

df1['UserLabel']
File "D:\script\Python279\lib\site-packages\pandas\core\frame.py", line 1787, in __getitem__
return self._getitem_column(key)
File "D:\script\Python279\lib\site-packages\pandas\core\frame.py", line 1794, in _getitem_column
return self._get_item_cache(key)
File "D:\script\Python279\lib\site-packages\pandas\core\generic.py", line 1079, in _get_item_cache
values = self._data.get(item)
File "D:\script\Python279\lib\site-packages\pandas\core\internals.py", line 2843, in get
loc = self.items.get_loc(item)
File "D:\script\Python279\lib\site-packages\pandas\core\index.py", line 1437, in get_loc
return self._engine.get_loc(_values_from_object(key))
File "pandas\index.pyx", line 134, in pandas.index.IndexEngine.get_loc (pandas\index.c:3824)
File "pandas\index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas\index.c:3704)
File "pandas\hashtable.pyx", line 697, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12349)
File "pandas\hashtable.pyx", line 705, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12300)
KeyError: 'UserLabel'

因为中间过程将df1.to_pickle成文件,一直以为是pickle问题,以为是Userlabel是Unicode导致的问题,最后细看pandas的api文档才发现这一切是因groupby()的默认参数所致。

An obvious one is aggregation via the aggregate or equivalently agg method:

In [40]: grouped = df.groupby('A')

In [41]: grouped.aggregate(np.sum)
Out[41]: 
            C         D
A                      
bar  0.443469  0.920834
foo  2.529056 -1.724719

In [42]: grouped = df.groupby(['A', 'B'])

In [43]: grouped.aggregate(np.sum)
Out[43]: 
                  C         D
A   B                        
bar one   -0.042379 -0.089329
    three -0.009920 -0.945867
    two    0.495767  1.956030
foo one   -0.556905 -1.113758
    three  1.548106 -0.016692
    two    1.537855 -0.594269

As you can see, the result of the aggregation will have the group names as the new index along the grouped axis. In the case of multiple keys, the result is a MultiIndex by default, though this can be changed by using the as_index option:

In [44]: grouped = df.groupby(['A', 'B'], as_index=False)

In [45]: grouped.aggregate(np.sum)
Out[45]: 
     A      B         C         D
0  bar    one -0.042379 -0.089329
1  bar  three -0.009920 -0.945867
2  bar    two  0.495767  1.956030
3  foo    one -0.556905 -1.113758
4  foo  three  1.548106 -0.016692
5  foo    two  1.537855 -0.594269

In [46]: df.groupby('A', as_index=False).sum()
Out[46]: 
     A         C         D
0  bar  0.443469  0.920834
1  foo  2.529056 -1.724719

Note that you could use the reset_index DataFrame function to achieve the same result as the column names are stored in the resulting MultiIndex:

as_index默认为true,由于groupby后,'Dn','UserLabel','BeginTime'都由column变成了index,多个index(MultiIndex),index无法用df1[列名]来表示
所以需要在groupby时加上as_index=False参数,或用reindex()
 
posted @ 2015-05-18 23:05  小风阵阵  阅读(18080)  评论(0编辑  收藏  举报