将标准差添加到 pandas groupby 对象的均值

3

I have a pandas dataframe, like the following:

import pandas as pd
df=pd.DataFrame(data=np.random.rand(10,5),columns=['blue','white','red','green','purple'])
df['group_labels']=['a','a','b','c','b','c','a','c','b','b']

我希望按照“group_labels”进行分组,计算平均值并在新的数据框中显示(平均值+-平均值的标准差)。因此,基本上我想要:

mean_df=df.groupby('group_labels').mean().reset_index()

但是,每个单元格中我还需要显示:
+- std deviation of the group / sqrt(size of the group)

可以吗?

请查看文档:http://pandas.pydata.org/pandas-docs/stable/groupby.html#applying-multiple-functions-at-once - EdChum
1个回答

8

我相信您需要使用DataFrameGroupBy.agg函数,该函数使用自定义函数创建,并由std函数进行操作,默认的ddof参数为1

np.random.seed(2019)
df=pd.DataFrame(data=np.random.rand(10,5),columns=['blue','white','red','green','purple'])
df['group_labels']=['a','a','b','c','b','c','a','c','b','b']


def func(x):
    return x.std() / len(x)**(1/2)

替代方案:

def func(x):
    return x.std() / np.sqrt(len(x))

df1 = df.groupby('group_labels').agg(['mean', func])
print (df1)
                  blue               white                 red            \
                  mean      func      mean      func      mean      func   
group_labels                                                               
a             0.450134  0.174723  0.401106  0.214163  0.417548  0.009156   
b             0.532030  0.185240  0.595667  0.174218  0.496617  0.150546   
c             0.552874  0.247173  0.382590  0.099883  0.571595  0.222161   

                 green              purple            
                  mean      func      mean      func  
group_labels                                          
a             0.786139  0.156584  0.525661  0.234515  
b             0.505838  0.215673  0.653970  0.114664  
c             0.653841  0.132705  0.587994  0.111854  

要在列中删除MultiIndex,请使用以下方法:

df1.columns = df1.columns.map('_'.join)
print (df1)
              blue_mean  blue_func  white_mean  white_func  red_mean  \
group_labels                                                           
a              0.702381   0.201604    0.679590    0.159292  0.743523   
b              0.386550   0.057390    0.418805    0.126278  0.306843   
c              0.636310   0.269986    0.385225    0.240675  0.451133   

              red_func  green_mean  green_func  purple_mean  purple_func  
group_labels                                                              
a             0.083068    0.788519    0.075999     0.738081      0.16673  
b             0.093714    0.792748    0.071369     0.465246      0.15333  
c             0.217406    0.293735    0.108021     0.549472      0.17632  

非常聪明,非常感谢。我没有考虑添加另一列。您认为是否可能将_mean和_func的元素统一到单个列中,其值为'mean +- std dev'? - sato
@sato - 我对于+和-有些困惑,你能解释一下吗? - jezrael
“±”是置信区间的符号。每当您提供科学测量时,必须以“值±标准差”的形式给出。我希望它们在一个单元格中,因此例如第一列的第一个单元格将是(0.70±0.20)。 - sato
@sato - 嗯,给我一点时间。 - jezrael
@sato - 可能是可行的,但不确定是否有用,因为需要把数字转换为字符串,并手动添加 +-,例如 df2 = (df1.xs('mean', axis=1, level=1).round(3).astype(str) + '±' + df1.xs('func', axis=1, level=1).round(3).astype(str)) - jezrael
1
好的,很酷,我不知道df.xs()函数。无论如何,非常感谢你的帮助! - sato

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接