how to make a Correlation One Column to Many Columns and return a list?

283 Views Asked by At

i would like to create a correlation function between a column and the others, passing the dataframe with all columns, corelating wiht a specif colum and returning a list of metrics and correlation i`am doing this like this.

correlations = df.corr().unstack().sort_values(ascending=True) 
correlations = pd.DataFrame(correlations).reset_index() 
correlations.columns = ['corr_matrix', 'dfbase', 'correlation'] 
correlations.query("corr_matrix == 'venda por m2' & dfbase != 'venda por m2'") 

enter image description here

but i would like to know a way to make this with a function.

1

There are 1 best solutions below

1
On BEST ANSWER

Something like this should do

def get_nonself_correlation(df,self_name):
    temp = df.corr()
    temp = temp.loc[temp.index!=self_name,temp.columns==self_name]
    temp = temp.unstack().reset_index()
    temp.columns = ['corr_matrix', 'dfbase', 'correlation'] 
    return temp