【问题标题】:SpecificationError: nested renamer is not supported error while using agg function on dictionarySpecificationError:在字典上使用 agg 函数时不支持嵌套重命名器错误
【发布时间】:2021-02-07 07:01:04
【问题描述】:

我正在使用 agg 函数并在我的数据框上使用参数作为字典。字典的代码是

aggregations = {
'Fare' : { # Work on the fare column
    'mean_Fare': 'mean',
    'median_Fare':'median',
    'max_Fare' : max,
    'min_Fare' : min
},
'Age' : { # Work on age column
    'median_Age' : 'median',
    'min_Age' : min,
    'max_Age' : max,
    'range_Age' : lambda x: max(x) - min(x) # calculate the age range per group
}

}

我的数据框中有 Fare 和 Age 列,在定义此字典后,我正在使用以下代码,这给了我错误

df.groupby(['Pclass']).agg(aggregations)

错误是

    ---------------------------------------------------------------------------
SpecificationError                        Traceback (most recent call last)
<ipython-input-44-28d5d263d58b> in <module>
----> 1 df.groupby(['Pclass']).agg(aggregations)

~\anaconda3\lib\site-packages\pandas\core\groupby\generic.py in aggregate(self, func, *args, **kwargs)
    926         func = _maybe_mangle_lambdas(func)
    927 
--> 928         result, how = self._aggregate(func, *args, **kwargs)
    929         if how is None:
    930             return result

~\anaconda3\lib\site-packages\pandas\core\base.py in _aggregate(self, arg, *args, **kwargs)
    340                     # {'ra' : { 'A' : 'mean' }}
    341                     if isinstance(v, dict):
--> 342                         raise SpecificationError("nested renamer is not supported")
    343                     elif isinstance(obj, ABCSeries):
    344                         raise SpecificationError("nested renamer is not supported")

SpecificationError: nested renamer is not supported

【问题讨论】:

    标签: python pandas dictionary aggregation


    【解决方案1】:

    正如错误所说,不支持嵌套重命名器。所以你需要像下面这样删除它:

    aggregations = {
        'Fare':{ # work on the "Fare" column
           'mean',  # get the mean fare
           'median', # get median fare
            max,
            np.min
        },
        'Age':{     # work on the "Age" column
            'median',   # Find the max, call the result "max_date"
             min,
             max,
             lambda x: max(x) - min(x)  # Calculate the age range per group
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-26
      • 1970-01-01
      • 2019-08-23
      • 1970-01-01
      • 2018-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多