Lets say I have the following DataFrame:
df = pd.DataFrame({'col1': [241, 123, 423], 'col2':[977, 78, np.NaN], 'col3':[76, 432, np.NaN], 'col4':[234, 321, 987]}, index=pd.date_range('2019-1-1', periods=3, freq="D")).rename_axis('Date')
which outputs:
col1 col2 col3 col4
Date
2019-01-01 241 977.0 76.0 234
2019-01-02 123 78.0 432.0 321
2019-01-03 423 NaN NaN 987
Another Dataframe, or even a Series, has the missing values for col2
and col3
. How can I replace the NaN
values with the values from df2
?
df2 = pd.DataFrame({'col2': 111, 'col3': 222}, index=[pd.to_datetime('2019-1-3')]).rename_axis('Date')
which looks like:
col2 col3
Date
2019-01-03 111 222
The final DataFrame I want should look like this:
col1 col2 col3 col4
Date
2019-01-01 241 977.0 76.0 234
2019-01-02 123 78.0 432.0 321
2019-01-03 423 111 222 987