I have two dataframes as below:
import pandas as pd
from numpy import nan
rawdata = {'col1': [3 ,nan ,4 ,7 ,nan ,5],
'col2': [10 ,20 ,10 ,30 ,10 ,40],
'col3': [23 ,34 ,45 ,56 ,34 ,23]}
fill_values = {'col1': [300 ,500]}
df1 = pd.DataFrame.from_dict(rawdata)
df2 = pd.DataFrame.from_dict(fill_values)
I want to fill nan
values in df1
Col1
with the ones in df2
from top to bottom, one by one.
I am sure that number of NaN
values if df1
equals to df2
's size.
Finally, I need to get below:
col1 col2 col3
3 10 23
300 20 34
4 10 45
7 30 56
500 10 34
5 40 23
You can try
df1.loc[df1['col1'].isnull(), 'col1'] = df2['col1'].values