Suppose I have the following dataframe:
pd.DataFrame({'col1': ["a", "a", np.nan, np.nan, np.nan],
'override1': ["b", np.nan, "b", np.nan, np.nan],
'override2': ["c", np.nan, np.nan, "c", np.nan]})
col1 override1 override2
0 a b c
1 a NaN NaN
2 NaN b NaN
3 NaN NaN c
4 NaN NaN NaN
Is there a way to collapse the 3 columns into one column, where override2
overrides override1
, which overrides col1
, however, in case there is NaN, then the values bofore is to be kept? Also, I am mainly looking for a way where I would not have to make an additional column. I am really looking for a built-in pandas solution.
This is the output I am looking for:
collapsed
0 c
1 a
2 b
3 c
4 NaN
using ffill
df.ffill(1).iloc[:,-1]
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