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 merge only on where condition

Please help with logic on applying merge function only where condition is met.

In below example: merge should be applicable only when, np.where name = John, else show 0

df1 = pd.DataFrame({'Name': ['John', 'Tom', 'Simon', 'Jose'], 
                    'Age': [5, 6, 4, 5]})



df2 = pd.DataFrame({'Name': ['John', 'Tom', 'Jose'],
                    'Class': ['Second', 'Third', 'Fifth']})

Expected result:
enter image description here

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

TIA

>Solution :

use merge and select the good rows of your df2.

df1.merge(df2[df2["Name"] == 'John'] , how = 'left' , on = 'Name')
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