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
JavaScript
x
5
1
Table_Name | Type
2
id | int
3
name | string
4
position| string
5
df2
JavaScript
1
4
1
Table_Name | Type
2
id | float
3
name | string
4
I want this to be the result.
df_result
JavaScript
1
4
1
Table_Name | Type
2
id | int
3
name | string
4
This is what i tried but it doesn’t work:
JavaScript
1
2
1
similar_cols = df1[df1['Table_name'].isin(df2['Table_name'])].dropna()
2
Advertisement
Answer
You need loc
here
JavaScript
1
2
1
similar_cols = df1.loc[df1['Table_name'].isin(df2['Table_name'])]
2