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

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.



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Arnau