【发布时间】: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