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
CodePudding user response:
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
CodePudding user response:
Found the solution here for anyone interested: Select rows from a Pandas DataFrame with exactly the same column values in another DataFrame
df1.merge(df2[['col2', 'col3']])