First non-null value per row from a list of Pandas columns

前端 未结 9 1232
难免孤独
难免孤独 2020-11-27 19:23

If I\'ve got a DataFrame in pandas which looks something like:

    A   B   C
0   1 NaN   2
1 NaN   3 NaN
2 NaN   4   5
3 NaN NaN NaN

How ca

9条回答
  •  庸人自扰
    2020-11-27 19:57

    Fill the nans from the left with fillna, then get the leftmost column:

    df.fillna(method='bfill', axis=1).iloc[:, 0]
    

提交回复
热议问题