Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas – get dataframe rows based on matching columns with other dataframe

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading