I am trying to loop through and store each columns cramer_v stat for categorical columns... The df only stores the last one.
for col in df2[columns_to_use].select_dtypes(include='category').columns[0:2]:
print(col)
cat_summary = pd.DataFrame({ 'column_name': col
,'dtypes': df2[col].dtypes
,'target_corr': cramerv_corrected(df2[col], df2[target].astype('category'))
})
updated the solution
'cat_summary' is reassigned in each iteration of the loop. Perhaps, try cat_summary[col].
Question need minimum reproducible example to reproduce and and validate the solution. However, this is what should help, based on details available
Create an empty dataframe, df3, then concat the dataframe you're creating within the loop. if you have a key (index), you need to introduce that in the concat to have a right join
df3 = pd.DataFrame()
for col in df2[columns_to_use].select_dtypes(include='category').columns[0:2]:
print(col)
cat_summary = pd.DataFrame({ 'column_name': col
,'dtypes': df2[col].dtypes
,'target_corr': cramerv_corrected(df2[col], df2[target].astype('category'))
})
df3 = pd.concat([df3, cat_summary], axis=1)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.