Pandas filter dataframe on multiple columns wrt corresponding column values from another dataframe 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']) &amp; df1['b'].isin(df2['b']) &amp; df1['c'].isin(df2['c']) &amp; 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.
You need to merge dataframes like done here in Pandas Merge. You also need to read on dataframe joins which is a common topic in learning databases.Here I have not done any tweaking with indices i.e, whether to keep left ones or right ones but you can check the docs for better information on here Merge Docs Pydata
import pandas as pd

columns = &quot;a b c d e f&quot;.split()
data = '''1 1 1 x 1 5
1 1 1 x 1 6
1 1 1 y 1 5
1 1 1 y 1 7'''.split(&quot;\n&quot;)

data = list(map(lambda x:x.split(), data ))


left = pd.DataFrame(columns = columns, data=data)



    a   b   c   d   e   f
0   1   1   1   x   1   5
1   1   1   1   x   1   6
2   1   1   1   y   1   5
3   1   1   1   y   1   7


right = pd.DataFrame(data = [&quot;1 1 1 x 1 5&quot;.split()], columns=columns)


    a   b   c   d   e   f
0   1   1   1   x   1   5


pd.merge(left, right, how=&quot;right&quot;, on=[&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;])

    a   b   c   d   e_x f_x e_y f_y
0   1   1   1   x   1   5   1   5
1   1   1   1   x   1   6   1   5


pd.merge(left, right, how=&quot;right&quot;, on=[&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;], suffixes=[&quot;&quot;, &quot;_&quot;] ).drop([&quot;e_&quot;, &quot;f_&quot;], axis=1)


    a   b   c   d   e   f
0   1   1   1   x   1   5
1   1   1   1   x   1   6