If I want to calculate the mean of two categories in Pandas, I can do it like this:
JavaScript
x
9
1
data = {'Category': ['cat2','cat1','cat2','cat1','cat2','cat1','cat2','cat1','cat1','cat1','cat2'],
2
'values': [1,2,3,1,2,3,1,2,3,5,1]}
3
my_data = DataFrame(data)
4
my_data.groupby('Category').mean()
5
6
Category: values:
7
cat1 2.666667
8
cat2 1.600000
9
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?
Advertisement
Answer
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:
JavaScript
1
8
1
from scipy.stats import ttest_ind
2
3
cat1 = my_data[my_data['Category']=='cat1']
4
cat2 = my_data[my_data['Category']=='cat2']
5
6
ttest_ind(cat1['values'], cat2['values'])
7
>>> (1.4927289925706944, 0.16970867501294376)
8
it returns a tuple with the t-statistic & the p-value
see here for other t-tests http://docs.scipy.org/doc/scipy/reference/stats.html