I have two dataframes:
d1 = {'id_': ['a','b','c','d'],
'year':['2018','2019','2017','2019']}
d2 = {'id_': ['a','c','e'],
'year':['2015',NaN,'2012']}
test1 = pd.DataFrame(d1)
test2 = pd.DataFrame(d2)
id_ year
0 a 2018
1 b 2019
2 c 2017
3 d 2019
id_ year
0 a 2015
1 c None
2 e 2012
I need to replace year values in test1 with year values from test2 only when id_ match. If the value is NaN, I'd like to keep the old value.
So the result looks like:
id_ year
0 a 2015
1 b 2019
2 c 2017
3 d 2019
All answers I came across were based on index or mapping old values to new values using dictionaries. I will appreciate your help.
set_indexandfillna?test2.combine_first(test1)?test1.fillna(test1,inplace=True)doesn't work?