简体   繁体   中英

Python Dataframe fill nan from multiple columns

I have a data frame with 3 columns. I want to fill nan in the first column with the second column. If there is also nan in the second, go to the third column.

My code:

xdf = pd.DataFrame({'A':[10,20,np.nan,np.nan],'B':[15,np.nan,30,np.nan],'C':[np.nan,np.nan,35,40]})

# fill nan in A
xdf['A'].fillna(xdf[['B','C']],inplace=True)

Present output:

TypeError: "value" parameter must be a scalar, dict or Series, but you passed a "DataFrame"

Expected output:

xdf = 
      A     B     C
0  10.0  15.0   NaN
1  20.0   NaN   NaN
2  30.0  30.0  35.0
3  40.0   NaN  40.0

Try via bfill() :

xdf['A']=xdf.bfill(1)['A']

output of df :

    A       B       C
0   10.0    15.0    NaN
1   20.0    NaN     NaN
2   30.0    30.0    35.0
3   40.0    NaN     40.0

Update:

if there were additional columns (like D, E) not needed to fillna then select the subset of df and backword fill on axis 1:

xdf['A']=xdf[['A','B','C']].bfill(1)['A']

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