1
votes

I have two dataframes (let's call them df1 and df2). I want to perform an inner join based on the index, but only take the columns from df1.

In SQL, it would be:

Select a.*
From df1 a
Inner join df2 b 
On a.index = b.index 

My code in Python is:

pd.concat([df1, df2], axis = 1, join = 'inner', join_axes = [df1.index])

But it selects all columns from both df1 and df2.

1

1 Answers

0
votes

One way you could do this to use [] after your pd.concat:

pd.concat([df1, df2], axis = 1, join = 'inner', join_axes = [df1.index])[df1.columns]