Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 

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?

like image 807
hirolau Avatar asked Nov 15 '12 19:11

hirolau


People also ask

What is T in Python pandas?

The T property is used to transpose index and columns. Reflect the DataFrame over its main diagonal by writing rows as columns and vice-versa. The property T is an accessor to the method transpose().

How do you find t statistic in Python?

T critical value can be found by using a T-distribution table or using statistical software. To find the T critical value, you need to specify the values: A critical level (q) (common values are 0.01, 0.05, and 0.10) The degrees of freedom (df)

What is the p value for t-test?

T-Values and P-values A p-value from a t test is the probability that the results from your sample data occurred by chance. P-values are from 0% to 100% and are usually written as a decimal (for example, a p value of 5% is 0.05). Low p-values indicate your data did not occur by chance.


Video Answer


1 Answers

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) 

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

like image 154
Gonzalo Avatar answered Sep 19 '22 15:09

Gonzalo