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

How to compare two data frame

df1

 index           Count
 Duliajan Area      2
 HAPJAN             2
 KATHALGURI         2

df2

 Location           Category
0        NAGAJAN        0
1        JORAJAN        0
2     KATHALGURI        0
3         HEBEDA        0
4          MAKUM        0
5       BAREKURI        0
6        BAGHJAN        0
7  Duliajan Area        0
8      LANGKASHI        0
9         HAPJAN        0

I need this output:

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

0        NAGAJAN        0
1        JORAJAN        0
2     KATHALGURI        2
3         HEBEDA        0
4          MAKUM        0
5       BAREKURI        0
6        BAGHJAN        0
7  Duliajan Area        2
8      LANGKASHI        0
9         HAPJAN        2

>Solution :

You can create dict from two columns of df1 then using map on the df2.

d = dict(zip(df1['index'], df1['Count']))
df2['Category'] = df2['Location'].map(d).fillna(df2['Category']).astype(int)
print(df2)

Output:

        Location  Category
0        NAGAJAN         0
1        JORAJAN         0
2     KATHALGURI         2
3         HEBEDA         0
4          MAKUM         0
5       BAREKURI         0
6        BAGHJAN         0
7  Duliajan Area         2
8      LANGKASHI         0
9         HAPJAN         2
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