I have the following dataframe:
A B C
0 NaN NaN cat
1 dog NaN NaN
2 NaN cat NaN
3 NaN NaN dog
I would like to add a colunm with the value that doesnt have the NaN
value. So that:
A B C D
0 NaN NaN cat cat
1 dog NaN NaN dog
2 NaN cat NaN cat
3 NaN NaN dog dog
would it be using an lambda
function? or fillna
? Any help would be appreciated! Thanks!
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
I would go with bfill
or ffill
on axis=1 here as QuangHoang suggests, however if there is 1 column having values always , another alternative with df.isna
, df.dot
and df.lookup
:
df['D'] = df.lookup(df.index,df.notna().dot(df.columns))
print(df)
A B C D
0 NaN NaN cat cat
1 dog NaN NaN dog
2 NaN cat NaN cat
3 NaN NaN dog dog
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With