I have a pandas dataframe, df1
:
a b c d e f
1 1 1 x 1 5
1 1 1 x 1 6
1 1 1 y 1 5
1 1 1 y 1 7
and another dataframe, df2
:
a b c d e f
1 1 1 x 1 5
Now I want to filter df1
on columns a, b, c, d
if it is present in respective columns of df2
. This is what I tried:
mask = df1['a'].isin(df2['a']) & df1['b'].isin(df2['b']) & df1['c'].isin(df2['c']) & df1['d'].isin(df2['d'])
df_new = df1[mask]
so df_new
finally contains:
a b c d e f
1 1 1 x 1 5
1 1 1 x 1 6
I am looking for a better approach as there are a lot of columns to compare and a lot of rows, both in df1
and df2
.