0

Assume two dataframes, one called df1 and the other df2:

df1 = pd.DataFrame.from_dict({'col1': [1,9,4,7],
                            'col2': [6,4,3,7],
                            'col3': [1,4,7,8]})

df2 = pd.DataFrame.from_dict({'col1': [4,8,2,7],
                            'col2': [7,3,3,3],
                            'col3': [2,7,7,5]})
df1:
   col1  col2  col3
0     1     6     1
1     9     4     4
2     4     3     7
3     7     7     8

df2:
   col1  col2  col3
0     4     7     2
1     8     3     7
2     2     3     7
3     7     3     5

As you can see, in both dataframes in 'col2', and 'col3' we have the combination (3, 7). I would like to iterate the rows of df1, and use col2 and col3 as a filter for df2 is such a way that for index (0, 1, 3) in df1 we will receive an empty dataframe, but for row 2 we will receive a dataframe with the rows of indexes (1,2) in df2 with its original indexes:

new dataframe:
       col1  col2  col3
    1     8     3     7
    2     2     3     7
Elad
  • 3
  • 2
  • The expected output is not clear, do you want a dataframe for each row in df1? Hence a list of dataframes? – Dani Mesejo Aug 07 '22 at 08:09
  • @DaniMesejo Yes I ended up taking the provided answer I got here, and turned the series that each row gave me into a dataframe to use in the provided answer. – Elad Aug 08 '22 at 09:04

1 Answers1

0

You can use pd.merge with how as inner

df2.reset_index().merge(df1[['col2','col3']], how="inner").set_index('index')
       col1  col2  col3
index                  
1         8     3     7
2         2     3     7
Himanshu Poddar
  • 7,112
  • 10
  • 47
  • 93