3

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?

0

2 Answers 2

4

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 (-:

Sign up to request clarification or add additional context in comments.

Comments

2

If df1 is only what you show, you can use map:

df2['country_code'] = df2['country_code'].map(df1.set_index('country_code')['country'])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.