How do I code Chi square loop code python

50 Views Asked by At

Would REALLY appreciate your help in writing a loop code for running chi square function on multiple columns (one group variable).I tried several options I found on line but they all failed, not sure if this is becasue I have NaN values since I am do not know how to incorporate it to the codele)

from scipy.stats import chisquare
objects_list=df[['Fulm_NF_Neg','HTN','DM','comp_risk_CV1yr']]

for column in objects_list:
    group1 = df.where(df.Sex== 0).dropna()[column]
    group2 = df.where(df.Sex== 1).dropna()[column]

df['p'] = chisquare(df[['Fulm_NF_Neg','HTN','DM','comp_risk_CV1yr']], axis=1)[1]

df['same_diff'] = np.where(df['p'] > 0.05, 'same', 'different')
0

There are 0 best solutions below