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?
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
(-:
如果df1
只是您显示的内容,则可以使用map
:
df2['country_code'] = df2['country_code'].map(df1.set_index('country_code')['country'])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.