Search code examples
pythonpandaspivotmeantruncated

Use stats.trim_mean ass aggfunc in Python Pandas Pivot


I'm trying to create a pivot with pandas where the aggregation function is truncated mean that takes 10% of each end of data.

    pivot = pd.pivot_table(data[(data['New_category_ID'] > 0)&(data['YYYY'] == 2016)], index = 'New_category',  values=['GrossRevenue_GBP','MOVC_GBP','PM_GBP'],aggfunc=stats.trim_mean(0.1))

I'm getting this following error:

TypeError: trim_mean() missing 1 required positional argument: 'proportiontocut'

stats.trim_mean() takes two arguments one being the data the other being the % of data to remove from each end of the data. In this case I'm specifying 10%.

How do I get around the need to specify the column/dataframe it should take the trimmed mean of given I am trying to create pivot table?

Would really appreciate some help


Solution

  • You can use a lambda function:

    pivot = pd.pivot_table(data[(data['New_category_ID'] > 0) & (data['YYYY']==2016)], 
                           index='New_category',
                           values=['GrossRevenue_GBP', 'MOVC_GBP', 'PM_GBP'],
                           aggfunc=lambda x: stats.trim_mean(x, 0.1))