-2

I have 2 dfs of different sizes. If the value df A matches df B, get the value from another column from df B to A.

Original df:

original_df = pd.DataFrame({'Patients': ['Kevin', 'John', 'Marry', 'Joe'],
                            'Region': ['New York', 'Austin', 'Dallas', 'Dallas']})

  Patients    Region
0    Kevin  New York
1     John    Austin
2    Marry    Dallas
3      Joe    Dallas

Region df:

region_df = pd.DataFrame({'Region': ['New York', 'Portland', 'Dallas', 'Austin', 'Vermont'],
                          'Lat': [10, 14, 13, 16, 15],
                          'Long': [19.1, 12.1, 16.2, 21.5, 29.3]})

     Region  Lat  Long
0  New York   10  19.1
1  Portland   14  12.1
2    Dallas   13  16.2
3    Austin   16  21.5
4   Vermont   15  29.3

Expected result df:

  Patients    Region  Lat  Long
0    Kevin  New York   10  19.1
1     John    Austin   16  21.5
2    Marry    Dallas   13  16.2
3      Joe    Dallas   13  16.2

Thank you so much

1 Answers1

1

You're trying to perform what's called a "left join":

>>> pd.merge(original_df, region_df, on="Region")
  Patients    Region  Lat  Long
0    Kevin  New York   10  19.1
1     John    Austin   16  21.5
2    Marry    Dallas   13  16.2
3      Joe    Dallas   13  16.2
ddejohn
  • 8,775
  • 3
  • 17
  • 30
  • Thank you very much – dotconnector Sep 18 '21 at 03:32
  • I still recommend looking at [Pandas Merging 101](https://stackoverflow.com/questions/53645882/pandas-merging-101) as it's an excellent resource, but it's also pretty dense, so I think it's fair to post exactly the answer you were looking for here. – ddejohn Sep 18 '21 at 03:33
  • I will sure do. This is an excellent community. I can't thank you enough – dotconnector Sep 18 '21 at 03:47