Reputation: 588
I have two dataframes df1
and df2
s = {'id': [4735,46,2345,8768,807],'city': ['a', 'b', 'd', 'e', 'f']}
s1 = {'id': [4735],'city_in_mail': ['x']}
df1 = pd.DataFrame(s)
df2 = pd.DataFrame(s1)
df1
looks like
id city
0 4735 a
1 46 b
2 2345 d
3 8768 e
4 807 f
and df2
looks like:
id city_in_mail
0 4735 x
I want to replace the value of column city
in dataframe df1
from the value of column city_in_mail
from dataframe df2
for the row where the id
value is same.
So my df1 should become:
id city
0 4735 x
1 46 b
2 2345 d
3 8768 e
4 807 f
How can do this with pandas ?
Upvotes: 3
Views: 389
Reputation: 153460
Try combine_first
with rename
to align column index:
df2.set_index('id')\
.rename(columns={'city_in_mail':'city'})\
.combine_first(df1.set_index('id'))\
.reset_index()
Output:
id city
0 4735.0 x
1 46.0 b
2 2345.0 d
3 8768.0 e
4 807.0 f
Note: You can reassign this back to df1 if you choose.
Upvotes: 3
Reputation: 59519
Also .map
+ .fillna
(if 'id'
is a unique key in df2
)
df1['city'] = df1.id.map(df2.set_index('id').city_in_mail).fillna(df1.city)
print(df1)
# id city
#0 4735 x
#1 46 b
#2 2345 d
#3 8768 e
#4 807 f
Upvotes: 3
Reputation: 323226
Using merge
with .loc
s=df1.merge(df2,how='outer')
s.loc[s.city_in_mail.notnull(),'city']=s.city_in_mail
s
city id city_in_mail
0 x 4735 x
1 b 46 NaN
2 d 2345 NaN
3 e 8768 NaN
4 f 807 NaN
Upvotes: 3
Reputation: 59274
Use indexes to match and then loc
df1 = df1.set_index('id')
df2 = df2.set_index('id')
df1.loc[df1.index.isin(df2.index), :] = df2.city_in_mail
Or use update
c = df1.city
c.update(df2.city_in_mail)
df1['city'] = c
All outputs
city
id
4735 x
46 b
2345 d
8768 e
807 f
Of course, feel free to do df1.reset_index()
in the end to go back to previous structure.
Upvotes: 3