mapping values from one dataframe to another dataframe

mHelpMe :

I have two dataframes.

df1 

country_code    country
US              USA
GB              Great Britain
DE              Germany

df2
country_code    date         rainfall
US              1990-01-01   235
GB              1990-01-01   235
DE              1990-01-01   235
US              1990-01-02   235
GB              1990-01-02   235
DE              1990-01-02   235
...
US              2020-01-01   235
GB              2020-01-01   235
DE              2020-01-01   235

I would like to know how to replace the values in df2 country_code to the corresponding values in df1 country.

ie. this is the desired output

country_code    date         rainfall
USA             1990-01-01   235
Great Britain   1990-01-01   235
Germany         1990-01-01   235
USA             1990-01-02   235
Great Britain   1990-01-02   235
Germany         1990-01-02   235
...
USA             2020-01-01   235
Great Britain   2020-01-01   235
Germany         2020-01-01   235

Basically how map values?

piRSquared :

Make a dictionary and map

mapper = dict(zip(df1.country_code, df1.country))
df2.assign(country_code=df2.country_code.map(mapper))

    country_code        date  rainfall
0            USA  1990-01-01       235
1  Great Britain  1990-01-01       235
2        Germany  1990-01-01       235
3            USA  1990-01-02       235
4  Great Britain  1990-01-02       235
5        Germany  1990-01-02       235
6            USA  2020-01-01       235
7  Great Britain  2020-01-01       235
8        Germany  2020-01-01       235

Per anky_91's suggestion:

We can also use replace this has the benefit of leaving elements alone that aren't in the mapper dictionary

mapper = dict(zip(df1.country_code, df1.country))
df2.assign(country_code=df2.country_code.replace(mapper))

However, we can also change mapper to do the same thing

mapper = lambda x: dict(zip(df1.country_code, df1.country)).get(x, x)
df2.assign(country_code=df2.country_code.map(mapper))

But this is exactly what Pandas does internally with replace (-:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=27736&siteId=1