I need to estimate the mean of a pandas DataFrameGroupBy by only considering the values between a given percentile range.
For instance, given the snippet
import numpy as np
import pandas as pd
a = np.matrix('1 1; 1 2; 1 4; 2 1; 2 2; 2 4')
data = pd.DataFrame(a)
groupby = data.groupby(0)
m1 = groupby.mean()
the result is
m1 = 1
0
1 2.333333
2 2.333333
However, if a percentile range is picked to exclude the maximum and minimum values the result should be
m1 = 1
0
1 2
2 2
How can I filter, for each group, the values between an arbitrary percentile range before estimating the mean? For instance, only considering the values between the 20th and 80th percentiles.