Let’s see how we can get the column-wise correlation between two data frames in R and in Python. Actually in R is straightforward and in Python we should do some tricks 🙂
Correlation Between two Data Frames in R
df1 = data.frame(x11 = c(10,20,30,40,50,55,60), x12 = c(11,15,20,30,35,60,70) ) df2 = data.frame(x21 = c(100,150,200,250,300,400,500), x22 = c(110,150,180,250,300,400,600) ) cor(df1,df2)
Output:
x21 x22
x11 0.9538727 0.9000503
x12 0.9889076 0.9755973
Correlation Between two Data Frames in Python
df1 = pd.DataFrame({'x11' : [10,20,30,40,50,55,60], 'x12' : [11,15,20,30,35,60,70]}) df2 = pd.DataFrame({'x21' : [100,150,200,250,300,400,500], 'x22' : [110,150,180,250,300,400,600]}) pd.concat([df1, df2], axis=1, keys=['df1', 'df2']).corr().loc['df1', 'df2']
Output:
x21 x22
x11 0.953873 0.900050
x12 0.988908 0.975597