plummms
plummms

Reputation: 25

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

Upvotes: 0

Views: 61

Answers (1)

BENY
BENY

Reputation: 323226

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

Upvotes: 1

Related Questions