Joining two dataframes on columns they match

Tags: , ,



I have two dataframes. df1 has more elements (3) in column ‘Table_name’ than df2 (2). I want a resultant dataframe that only outputs the rows where df1 and df2 share the same column names.

df1

Table_Name | Type
   id      | int
   name    | string
   position| string

df2

Table_Name | Type
   id      | float
   name    | string

I want this to be the result.

df_result

Table_Name | Type
   id      | int
   name    | string

This is what i tried but it doesn’t work:

similar_cols = df1[df1['Table_name'].isin(df2['Table_name'])].dropna()

Answer

You need loc here

similar_cols = df1.loc[df1['Table_name'].isin(df2['Table_name'])]


Source: stackoverflow