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
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…