Group by groups to Pandas Series/Dataframe

主宰稳场 提交于 2019-12-08 20:48:26

See Welch's T-Test

I'd do it like this:

def welch_ttest(x1, x2):
    x_1 = x1.mean()
    x_2 = x2.mean()
    s1 = x1.std()
    s2 = x2.std()
    n1 = len(x1)
    n2 = len(x2)
    return ((x_1 - x_2) / (np.sqrt(s1 ** 2 / n1 + s2 ** 2 / n2)))

def grouped_welch_ttest(df):
    return welch_ttest(df.key2, df.key3)

df1.groupby('key1').apply(grouped_welch_ttest)

key1
0   -1.471497
1    1.487045
dtype: float64
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!