简体   繁体   中英

Fill null values in a dataframe from another sized dataframe one by one

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

你可以试试

df1.loc[df1['col1'].isnull(), 'col1'] = df2['col1'].values

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM