How to combine three string columns to one which have Nan values in Pandas

use combine_first chained

df['D'] = df.A.combine_first(df.B).combine_first(df.C)

alternatively, forward fill and pick the last column

df['D'] = df.ffill(axis=1).iloc[:,-1]

# specifying the columns explicitly:
df['D'] = df[['A', 'B', 'C']].ffill(1).iloc[:, -1]

Please try

df['D']=df.stack().reset_index(drop=True)

Let's try bfill:

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

Output:

     A    B    C    D
0  NaN  NaN  cat  cat
1  dog  NaN  NaN  dog
2  NaN  cat  NaN  cat
3  NaN  NaN  dog  dog

Tags:

Python

Pandas