Predictive Hacks

How to get the Correlation between two Data Frames

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

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Python

Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.

Python

Intro to Chatbots with HuggingFace

In this tutorial, we will show you how to use the Transformers library from HuggingFace to build chatbot pipelines. Let’s