Reputation: 2807
I have two pandas data frame as below,
Left_Frame
symbol price timelab1 timelab2 timelab3 _merge
0 145822 10.5018 19:00 left_only
1 145819 10.5055 19:00 left_only
Right_Frame
symbol price timelab1 timelab2 timelab3 _merge
16 145822 10.58 NaN NaN NaN right_only
17 145819 10.55 NaN NaN NaN right_only
I want to update Left_Frame
price with Right_Frame
price where symbol
are equal to the both data frames.
Expected result is,
Left_Frame
symbol price timelab1 timelab2 timelab3 _merge
0 145822 10.58 19:00 left_only
1 145819 10.55 19:00 left_only
How can I do this in Pandas?
Upvotes: 0
Views: 43
Reputation: 3770
this would do the job
I want to update Left_Frame price with Right_Frame price where symbol are equal to the both data frames. Expected result is
left_frame['price'] = np.where(left_frame['symbol'].values == right_frame['symbol'].values, right_frame['price'],left_frame['price'])
Update
left_frame.loc[:,'price'] = np.where(left_frame['symbol'].values == right_frame['symbol'].values, right_frame['price'],left_frame['price'])
Upvotes: 2
Reputation: 862681
Use Series.map
by another Series
created by DataFrame.set_index
and for unmatched values replace by original by Series.fillna
:
print (Left_Frame)
symbol price timelab1 timelab2 timelab3 _merge
0 145822 12 19:00 NaN NaN left_only
1 100000 13 19:00 NaN NaN left_only
print (Right_Frame)
symbol price timelab1 timelab2 timelab3 _merge
16 145822 10 NaN NaN NaN right_only
17 145819 18 NaN NaN NaN right_only
s = Right_Frame.set_index('symbol')['price']
Left_Frame['price'] = Left_Frame['symbol'].map(s).fillna(Left_Frame['price'])
#slowier alternative
#Left_Frame['price'] = Left_Frame['symbol'].replace(s)
print (Left_Frame)
symbol price timelab1 timelab2 timelab3 _merge
0 145822 10.0 19:00 NaN NaN left_only
1 100000 13.0 19:00 NaN NaN left_only
Upvotes: 1