I am grouping by item-date pairs in a PD dataframe and would like to add some custom conditional functions using lambda to a larger aggregation function.
Using the tip here, I can do the following, which works properly and counts positive and negative values in the given column.
item_day_count=item_day_group['PriceDiff_pct'].agg({'Pos':lambda val: (val > 0).sum(),'Neg':lambda val: (val <= 0).sum()}).reset_index()
And I can also do a different aggregate that contains both pre-built aggregations and a custom percentile function that returns proper stats:
item_day_count_v2=item_day_group['PriceDiff_pct'].agg(['count','min',percentile(25),'mean','median',percentile(75),'max']).reset_index()
But I cannot figure out how to combine these into one larger function - I get the error : AttributeError: 'DataFrameGroupBy' object has no attribute 'name'
when I try the below:
item_day_count_v3=item_day_group['PriceDiff_pct'].agg(['count',{'Pos_Return':lambda val: (val > 0).sum(),'Neg_Return':lambda val: (val <= 0).sum()},'min',percentile(25),'mean','median',percentile(75),'max']).reset_index()
Does anyone know how to combine these functions? Seems like it I am close considering both work separately. Thanks for your help!