Reputation: 31
I am using agg function and using parameter as a dictionary on my dataframe. The code for dictionary is
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
}
}
I have columns as Fare and Age in my dataframe and after defining this dictionary I am using the below code, which is giving me error
df.groupby(['Pclass']).agg(aggregations)
The error is
---------------------------------------------------------------------------
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
Upvotes: 3
Views: 2458
Reputation: 31
As the error says, nested renamer is not supported. So you need to remove it like below:
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
}
}
Upvotes: 3