how to collapse columns in pandas on null values?

后端 未结 6 2003
醉酒成梦
醉酒成梦 2021-01-14 02:18

Suppose I have the following dataframe:

pd.DataFrame({\'col1\':    [\"a\", \"a\", np.nan, np.nan, np.nan],
            \'override1\': [\"b\", np.nan, \"b\",          


        
6条回答
  •  甜味超标
    2021-01-14 02:56

    With focus on performance, here's one with NumPy -

    In [106]: idx = df.shape[1] - 1 - df.notnull().to_numpy()[:,::-1].argmax(1)
    
    In [107]: pd.Series(df.to_numpy()[np.arange(len(df)),idx])
    Out[107]: 
    0      c
    1      a
    2      b
    3      c
    4    NaN
    dtype: object
    

提交回复
热议问题