Use pd.concat to internally join two dataframes and select columns from only one dataframe… here is a solution to the problem.
Use pd.concat to internally join two dataframes and select columns from only one dataframe
I have two dataframes (we call them df1 and df2). I want to perform an inner join based on an index, but only get the columns from df1.
In SQL, it will be:
Select a.*
From df1 a
Inner join df2 b
On a.index = b.index
My Python code is:
pd.concat([df1, df2], axis = 1, join = 'inner', join_axes = [df1.index])
But it selects all columns from df1 and df2.
Solution
One way is to use []:: after pd.concat
pd.concat([df1, df2], axis = 1, join = 'inner', join_axes = [df1.index])[df1.columns]