0

Data:

    import numpy as np
    import pandas as pd

    d1 = pd.DataFrame({"Name" : ['Kate', 'Jessica', 'Don', 'John', 'Gino', 'Luca', 'Alejandra', 'Dirk'],
               "Country": ['United States', 'United States', 'United Kingdom', 'South Africa', 'Italy', 'Croatia', 'Mexico', 'Germany']})
    d2 = pd.DataFrame({ "Country": ['United States','United Kingdom', 'South Africa', 'Italy', 'Croatia', 'Mexico'],
                "Abbreviation": ['USA', 'UK', 'RSA', 'ITA', 'HRV', 'MEX']})

    d1

        Name      Country
    0   Kate      United States
    1   Jessica   United States
    2   Don       United Kingdom
    3   John      South Africa
    4   Gino      Italy
    5   Luca      Croatia
    6   Alejandra Mexico
    7   Dirk      Germany

    d2

       Country         Abbreviation
    0  United States   USA
    1  United Kingdom  UK
    2  South Africa    RSA
    3  Italy           ITA
    4  Croatia         HRV
    5  Mexico          MEX

I want to add a column called "Abbreviation" to d1 whose values consist of the abbreviation (pulled from d2) corresponding to the value in the "Country" column:

    d1

        Name      Country         Abbreviation
    0   Kate      United States   USA
    1   Jessica   United States   USA
    2   Don       United Kingdom  UK
    3   John      South Africa    RSA
    4   Gino      Italy           ITA
    5   Luca      Croatia         HRV
    6   Alejandra Mexico          MEX
    7   Dirk      Germany         NaN

So Kate in d1 (first row) will get the value "USA" in the new "Abbreviation" column because the abbreviation for "United States" in d2 is "USA".

Note that Jessica (second row in d1) also gets "USA" even though "USA" only appears once in d2. Additionally, because "Germany" is not an entry in d2, Dirk in d1 gets NaN (or an equivalent type).

Thank you!

user603535
  • 55
  • 5

0 Answers0