zoukankan      html  css  js  c++  java
  • Dataframe操作时出现 KeyError: '列名xxx'

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

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

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

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

    出现

    df1['UserLabel']
    File "D:scriptPython279libsite-packagespandascoreframe.py", line 1787, in __getitem__
    return self._getitem_column(key)
    File "D:scriptPython279libsite-packagespandascoreframe.py", line 1794, in _getitem_column
    return self._get_item_cache(key)
    File "D:scriptPython279libsite-packagespandascoregeneric.py", line 1079, in _get_item_cache
    values = self._data.get(item)
    File "D:scriptPython279libsite-packagespandascoreinternals.py", line 2843, in get
    loc = self.items.get_loc(item)
    File "D:scriptPython279libsite-packagespandascoreindex.py", line 1437, in get_loc
    return self._engine.get_loc(_values_from_object(key))
    File "pandasindex.pyx", line 134, in pandas.index.IndexEngine.get_loc (pandasindex.c:3824)
    File "pandasindex.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandasindex.c:3704)
    File "pandashashtable.pyx", line 697, in pandas.hashtable.PyObjectHashTable.get_item (pandashashtable.c:12349)
    File "pandashashtable.pyx", line 705, in pandas.hashtable.PyObjectHashTable.get_item (pandashashtable.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()
     
  • 相关阅读:
    268. Missing Number(LeetCode)
    Win7 发生验证错误 要求的函数不受支持
    下拉选择框的选项过滤
    金额小写带逗号处理
    Java将CST的时间字符串转换成需要的日期格式字符串
    MyEclipse部署项目报"Add Deployment". Invalid Subscription Level
    无法解析的DNS服务地址
    选择文件进行上传
    让button居中显示的的标签
    Mysql启动服务提示系统找不到指定的文件
  • 原文地址:https://www.cnblogs.com/lightwind/p/4513324.html
Copyright © 2011-2022 走看看