天天看點

Dataframe groupby 操作之後取列值時出現 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()