-1

I'm trying to join 2 dataframes and I am struggling with the merge() function. I have a df of country names and populations for 2 periods called pop:

                                  Country    1990    2019
0                             Afghanistan    12.4      38
1                                 Albania     3.3     2.9
2                                 Algeria    25.8    43.1
3                                 Andorra     0.1     0.1
4                                  Angola    11.8    31.8
5                     Antigua and Barbuda     0.1     0.1
6                               Argentina    32.6    44.8
7                                 Armenia     3.5       3

And a df called countryCode:

   Code                                       Country
1    AFG                                   Afghanistan 
5    ALB                                       Albania 
63   DZA                                       Algeria 
11   ASM                                American Samoa 
6    AND                                       Andorra 
2    AGO                                        Angola 
3    AIA                                      Anguilla 
12   ATA                                    Antarctica 

I'd like to merge pop and countryCode by country name and exclude any rows, like Antarctica, where I have no population data. Once I am sure the country code matches the country name from pop I will remove the duplicate Country column.

Ultimately I want it to look something like this :

                                  Country    1990    2019    Code
0                             Afghanistan    12.4      38   AFG
1                                 Albania     3.3     2.9   ALB
2                                 Algeria    25.8    43.1   DZA
3                                 Andorra     0.1     0.1   AND
4                                  Angola    11.8    31.8   AGO
Mercredi
  • 41
  • 5

1 Answers1

2
df1.merge(countryCode, on='Country')

Where df1 is the name of the first DataFrame.

geo199
  • 44
  • 5
  • I see you're new here. Welcome to SO! For future reference, it's generally frowned upon to post an answer which is identical to one that already exists. – ddejohn Oct 11 '21 at 23:28