我的数据看起来是这样的:
exp itr res1
e01 1 20
e01 2 21
e01 3 22
e01 4 23
e01 5 24
e01 6 25
e01 7 26
e01 8 27
e02 . .
e02 . .我必须根据itr将数据分成两组,一组为itr 1-4,一组为itr 5-8。
然后我要计算这两组的t检验:
我目前的代码是:
data_top4=data.groupby('exp').head(4)
data_bottom4=data.groupby('exp').tail(4)
tt_df.groupby('exp').apply(lambda df:
stats.ttest_ind(data.groupby('exp').head(4), data.groupby('exp').tail(4)
[0])它不能正常工作并且有错误!
发布于 2018-09-28 11:42:20
您可以使用自定义函数:
from scipy.stats import ttest_ind
def f(x):
cat1_1 = x.head(4)
cat1_2 = x.tail(4)
t, p = ttest_ind(cat1_1['res1'], cat1_2['res1'])
return pd.Series({'t':t, 'p':p})
out = data.groupby('exp').apply(f)
print (out)
t p
exp
e01 -4.38178 0.004659编辑:
def f(x):
cat1_1 = x.head(4)
cat1_2 = x.tail(4)
t, p = ttest_ind(cat1_1, cat1_2)
return pd.Series({'t':t, 'p':p})
out = data.groupby('exp')['res1'].apply(f).unstack()
print (out)
t p
exp
e01 -4.38178 0.004659或者:
def f(x, col):
cat1_1 = x.head(4)
cat1_2 = x.tail(4)
t, p = ttest_ind(cat1_1[col], cat1_2[col])
return pd.Series({'t':t, 'p':p})
out = data.groupby('exp').apply(f, 'res1')
print (out)
t p
exp
e01 -4.38178 0.004659https://stackoverflow.com/questions/52554488
复制相似问题