How to calculate p-values for pairwise correlation of columns in Pandas?

后端 未结 4 1014
执念已碎
执念已碎 2021-02-09 11:01

Pandas has the very handy function to do pairwise correlation of columns using pd.corr(). That means it is possible to compare correlations between columns of any length. For in

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-09 11:42

    This will work:

    from scipy.stats import pearsonr
    
    column_values = [column for column in df.columns.tolist() ]
    
    
    df['Correlation_coefficent'], df['P-value'] = zip(*df.T.apply(lambda x: pearsonr(x[column_values ],x[column_values ])))
    df_result = df[['Correlation_coefficent','P-value']]
    

提交回复
热议问题