How to create a for loop in a DataFrame

Viewed 29

Hello I am very new to programming

I have a big dataframe from which I want to run a t student using test_ind which performs a mean difference of quantitative variable of two groups. For example comparing the oxygen levels of a control group vs a treatment group The thing is that I have a lot of quantitative variables to run and I want to develop a function which

  1. Takes the data of the groups
  2. Runs the t stat
  3. Put the data in a data frame

I have this code but I do not know how to add a loop into it

#This part grabs the variables from the dataframe
grupoA= df[df['Grupo ']=='A']['Sat de oxigeno 3']
grupoB= df[df['Grupo ']=='B']['Sat de oxigeno 3']

#This one runs the t stat 
x18=ttest_ind(grupoA, grupoB)

#This one puts the results in a dataframe
tablaT = pd.DataFrame((x7,x8,x9, x10, x11,x12,x13,x14,x15,x16,x17,x18),columns='T-test p-value'.split(),index='Frecuencia1 Frecuencia2 Frecuencia3 PresionSistolica1 PresionSistolica2 PresionSistolica3 PresionDiastolica1 PresionDiastolica2 PresionDiastolica3 SaturacionOx1 SaturacionOx2 SaturacionOx3'.split())
tablaT  ```

The thing is that I would like to have a function that I enter the variable from the dataframe and runs the code instead of doing it constantly 

Thank you very much 
2 Answers

If I understand correctly, we can use a nested dict comprehension to calculate the T-Test for every possible combination of the groups of datas/measures and then use pandas.DataFrame.from_dict to get the final dataframe :

from scipy.stats import ttest_ind
import pandas as pd
import numpy as np
import re

df = pd.read_excel("basededatoslamaga-ejemplo.xlsx")
df.columns = [re.sub(' +', ' ', col.strip()) for col in df.columns]

list_of_measures = sorted(df.filter(regex='^Frecuencia|Presion|Sat').columns.tolist())

dico_v1 = {x: 's={:.6f}, p={:.6f}'.format(
          *ttest_ind(df[df['Grupo']=='A'][x], df[df['Grupo']=='B'][x])) for x in list_of_measures}

dico_v2 = {key: list(map(str, re.sub('s=|p=', '', value).replace(' ', '').split(','))) for key, value in dico_v1.items()}

result_df = pd.DataFrame.from_dict(dico_v2, orient='index', columns=['T-test', 'p-value'], dtype=np.float64)

# Output :

print(result_df)

                        T-test   p-value
Frecuencia 1         -0.321216  0.749534
Frecuencia 2          1.085525  0.283470
Frecuencia 3         -1.365494  0.178885
Presion diastolica 1 -0.071333  0.943449
Presion diastolica 2 -1.372688  0.176652
Presion diastolica 3 -0.448372  0.656036
Presion sistolica    -0.131486  0.895977
Presion sistolica 2   1.610477  0.114287
Presion sistolica 3  -0.462267  0.646117
Sat de oxigeno       -0.471234  0.639750
Sat de oxigeno 3      0.603149  0.549438
Sat oxigeno 2        -0.023978  0.980976

If a function is needed, you can try with this one :

def ttest_groups(df, grp1, grp2):
    list_of_measures = sorted(df.filter(regex='^Frecuencia|Presion|Sat').columns.tolist())
    dico_v1 = {x: 's={:.5f}, p={:.5f}'.format(
              *ttest_ind(df[df['Grupo']==grp1][x], df[df['Grupo']==grp2][x])) for x in list_of_measures}
    
    dico_v2 = {key: list(map(str, re.sub('s=|p=', '', value).replace(' ', '').split(','))) for key, value in dico_v1.items()}

    result_df = pd.DataFrame.from_dict(dico_v2, orient='index', columns=['T-test', 'p-value'], dtype=np.float64)
    
    return result_df

ttest_groups(df, 'A', 'B')
Related