Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Correlation of every pandas row with another pandas dataframe as a new column

Assuming I have the following df:

Company   Apples   Mangoes   Oranges

Amazon       0.75      0.6     0.98
BellTM       0.23      0.75    0.14
Cadbury      0.4       0.44    0.86

and then another data frame called vendor:

Company   Apples   Mangoes   Oranges

Deere       0.11      0.3     0.79

I want to find the row-wise correlation of each company with the company Deere in the vendor data frame. I want the outputted correlation coefficient added as a column called Correlationcoef to the original data frame df:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Company   Apples   Mangoes   Oranges     Corrcoef

Amazon       0.75      0.6     0.98     0.77955981 
BellTM       0.23      0.75    0.14    -0.37694478
Cadbury      0.4       0.44    0.86     0.98092707

When I attempt the following:

df.iloc[:,1:].corrwith(vendor.iloc[:,1:], axis=1)

I get a list with NaN values.
I obtained the Corrcoef values manually by saving each row as an array and using np.corrcoef(x1,y)

>Solution :

You need to use a Series in corrwith.

You can use:

df.set_index('Company').corrwith(vendor.set_index('Company').loc['Deere'], axis=1)

output:

Company
Amazon     0.779560
BellTM    -0.376945
Cadbury    0.980927
dtype: float64

With your code:

df.iloc[:, 1:].corrwith(vendor.iloc[0,1:].astype(float), axis=1)

output:

0    0.779560
1   -0.376945
2    0.980927
dtype: float64
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading