Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to collapse columns in pandas on null values?

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
like image 837
callmeGuy Avatar asked Nov 29 '22 21:11

callmeGuy


1 Answers

using ffill

df.ffill(1).iloc[:,-1]
like image 113
BENY Avatar answered Dec 04 '22 09:12

BENY