熊猫groupby温莎

时间:2019-12-09 02:57:56

标签: python pandas

正常的分组平均很容易:

df.groupby(['col_a','col_b']).mean()[col_i_want]

但是,如果我想应用一个均化的均值(默认限制为0.05和0.95),这等同于裁剪数据集然后执行均值,突然似乎没有简单的方法呢?我将必须:

winsorized_mean = []
col_i_want = 'col_c'
for entry in df['col_a'].unique():
    for entry2 in df['col_b'].unique():
        sub_df = df[(df['col_a'] == entry) & (df['col_b'] == entry2)]
        m = sub_df[col_to_groupby].clip(lower=0.05,upper=0.95).mean()
        winsorized_mean.append([entry,entry2,m])

是否存在我不知道自动执行的功能?

1 个答案:

答案 0 :(得分:3)

您可以使用scipy.stats.trim_mean

import pandas as pd
from scipy.stats import trim_mean

# label 'a' will exhibit different means depending on trimming
label = ['a'] * 20 + ['b'] * 80 + ['c'] * 400 + ['a'] * 100

data = list(range(100)) + list(range(500, 1000))

df = pd.DataFrame({'label': label, 'data': data})

grouped = df.groupby('label')

# trim 5% off both ends
print(grouped.apply(stats.trim_mean, .05))

# trim 10% off both ends
print(grouped.apply(stats.trim_mean, .1))