简体   繁体   English

Pandas 中的 T 检验

[英]T-test in Pandas

If I want to calculate the mean of two categories in Pandas, I can do it like this:如果我想计算 Pandas 中两个类别的平均值,我可以这样做:

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

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.我有很多这样格式化的数据,现在我需要做一个T检验,看看cat1cat2的平均值在统计上是否不同。 How can I do that?我怎样才能做到这一点?

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:这取决于你想做什么样的 t 检验(一侧或两侧依赖或独立),但它应该像这样简单:

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)

it returns a tuple with the t-statistic & the p-value它返回一个带有 t 统计量和 p 值的元组

see here for other t-tests http://docs.scipy.org/doc/scipy/reference/stats.html有关其他 t 测试,请参见此处http://docs.scipy.org/doc/scipy/reference/stats.html

EDIT: I had not realized this was about the data format.编辑:我没有意识到这是关于数据格式的。 You could use可以用

import pandas as pd
import scipy
two_data = pd.DataFrame(data, index=data['Category'])

Then accessing the categories is as simple as然后访问类别就像

scipy.stats.ttest_ind(two_data.loc['cat'], two_data.loc['cat2'], equal_var=False)

The loc operator accesses rows by label. loc operator按标签访问行。


As @G Garcia said正如@G Garcia 所说

one sided or two sided dependent or independent一侧或两侧依赖或独立

If you have two independent samples but you do not know that they have equal variance , you can use Welch's t-test .如果您有两个独立样本,但您不知道它们的方差相等,则可以使用Welch 的 t-test It is as simple as就这么简单

scipy.stats.ttest_ind(cat1['values'], cat2['values'], equal_var=False)

For reasons to prefer Welch's test, see https://stats.stackexchange.com/questions/305/when-conducting-at-test-why-would-one-prefer-to-assume-or-test-for-equal-vari .有关偏爱 Welch 测试的原因,请参阅https://stats.stackexchange.com/questions/305/when-conducting-at-test-why-would-one-prefer-to-assume-or-test-for-equal-变种

For two dependent samples , you can use对于两个相关样本,您可以使用

scipy.stats.ttest_rel(cat1['values'], cat2['values'])

I simplify the code a little bit.我稍微简化了代码。

from scipy.stats import ttest_ind
ttest_ind(*my_data.groupby('Category')['value'].apply(lambda x:list(x)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM