简体   繁体   English

从另一个大小的数据框中一一填充数据框中的空值

[英]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. 我想用顶部和底部的df2中的df2一一填充df1 Col1 nan值。

I am sure that number of NaN values if df1 equals to df2 's size. 如果df1等于df2的大小,我可以确定NaN值的数量。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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