0

I have a simple question. I have 2 dataframe:

df1 =

code countries
FR France
US United-States
IT Italy

and an other one which is:

df2=

countries
FR
FR
IT
US
US
US
IT

I would like in df2 replace the countries column with the one in df1.

iamwillbin
  • 55
  • 3
jacques
  • 47
  • 4

3 Answers3

1

Merge with code on df1 and countries on df2 with a bit of renaming and then just grab the countries column.

df2 = df2.merge(df1, right_on = 'code', left_on = 'countries', suffixes = ('_old', ''))
df2 = df2[['countries']]
Michael Cao
  • 2,278
  • 1
  • 1
  • 13
1

map the values using the dict form of df1:

>>> df2["countries"].map(df1.set_index("code").squeeze().to_dict())

0           France
1           France
2            Italy
3    United-States
4    United-States
5    United-States
6            Italy
Name: countries, dtype: object
not_speshal
  • 22,093
  • 2
  • 15
  • 30
0

You can use merge function, then rename and drop extra columns. I added some extra actions with index to restore it after merging.

df1 = pd.DataFrame({
    "code": ["FR", "US", "IT"],
    "countries": ["France", "United-States", "Italy"]
})

df2 = pd.DataFrame({
    "countries": ['FR', 'FR', 'IT', 'US', 'US', 'US', 'IT'],
    "idx": range(7),
})

df2.reset_index(inplace=True)

df2 \
    .merge(df1, left_on="countries", right_on="code") \
    .rename({"countries_y": "countries"}, axis=1) \
    .set_index("index") \
    .drop(["code", "countries_x"], axis=1)

Output:

           countries
index               
0             France
1             France
2              Italy
6              Italy
3      United-States
4      United-States
5      United-States
Maria K
  • 1,491
  • 1
  • 3
  • 14