0

My dataframe looks like this:

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     .       .

I have to split data in to two groups based on itr ie itr 1-4 in one group and itr 5-8 in oter group

Then I have to calculate t-test on this two groups:

My current code is:

 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])

It doesn't function properly and has errors!

TjS
  • 277
  • 2
  • 5
  • 16

1 Answers1

1

You can use custom function:

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

EDIT:

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

Or:

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.004659
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • What do I need to do if I need to pass 1 more argument to a function. I need to pass ['res1'] as argument to function – TjS Sep 28 '18 at 13:39
  • @tejasshah - Check edited answer - is possible define columns name for check after groupby in second solution or pass value in apply like in last one. – jezrael Sep 28 '18 at 13:43