简体   繁体   中英

Merging two columns with non-unique rows and NaNs in pandas

I have two columns in a dataframe df:

   A    B
0  NaN  NaN
1  3.14 NaN
2  NaN  4.20
3  3.65 0.68

Intended result for df:

   A    B    C
0  NaN  NaN  NaN
1  3.14 NaN  3.14
2  NaN  4.20 4.20
3  3.65 0.68 3.65

What's the pandas equivalent for?

if(A == np.nan):
    if(B == np.nan):
        C = np.nan
    else: 
        C == B
else:
    C = A

Check with bfill

df['C']=df.bfill(1).iloc[:,0]

df
      A     B     C
0   NaN   NaN   NaN
1  3.14   NaN  3.14
2   NaN  4.20  4.20
3  3.65  0.68  3.65

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