Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compute correlation of two DataFrames columnwise

I have two DataFrames and I want to compute their correlations without looping:

import pandas as pd
df1 = pd.DataFrame({'A': range(0,4), 'B': range(14,10,-1)})
df2 = pd.DataFrame({'C': range(104,100,-1), 'D': range(2,6), 'E': range(11,7,-1)})
corr = pd.DataFrame(dict(c1=c1, **{c2:df2[c2].corr(df1[c1]) for c2 in df2.columns})
                    for c1 in df1.columns).set_index("c1")
corr.index.name = None

Now corr is

     C    D    E
A -1.0  1.0 -1.0
B  1.0 -1.0  1.0

Neither DataFrame.corr nor DataFrame.corrwith do what I need.

like image 390
sds Avatar asked Oct 16 '25 20:10

sds


1 Answers

You can use the methods apply and corrwith:

df2.apply(df1.corrwith)

Output:

     C    D    E
A -1.0  1.0 -1.0
B  1.0 -1.0  1.0
like image 97
Mykola Zotko Avatar answered Oct 18 '25 09:10

Mykola Zotko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!