pandas T检验 [英] T-test in Pandas
问题描述
如果我想计算熊猫中两个类别的平均值,我可以这样做:
If I want to calculate the mean of two categories in Pandas, I can do it like this:
data = {'Category': ['cat2','cat1','cat2','cat1','cat2','cat1','cat2','cat1','cat1','cat1','cat2'],
'values': [1,2,3,1,2,3,1,2,3,5,1]}
my_data = DataFrame(data)
my_data.groupby('Category').mean()
Category: values:
cat1 2.666667
cat2 1.600000
我用这种方式格式化了很多数据,现在我需要做一个 T 测试,看一下 cat1 和 cat2 在统计上是不同的.我该怎么办?
I have a lot of data formatted this way, and now I need to do a T-test to see if the mean of cat1 and cat2 are statistically different. How can I do that?
推荐答案
这取决于您要进行哪种t检验(单侧或双侧独立或独立),但是它应该像这样简单:
it depends what sort of t-test you want to do (one sided or two sided dependent or independent) but it should be as simple as:
from scipy.stats import ttest_ind
cat1 = my_data[my_data['Category']=='cat1']
cat2 = my_data[my_data['Category']=='cat2']
ttest_ind(cat1['values'], cat2['values'])
>>> (1.4927289925706944, 0.16970867501294376)
返回具有t统计量&的元组. p值
it returns a tuple with the t-statistic & the p-value
有关其他t检验,请参见此处 http://docs.scipy.org /doc/scipy/reference/stats.html
see here for other t-tests http://docs.scipy.org/doc/scipy/reference/stats.html
这篇关于 pandas T检验的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!