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

前端 未结 9 1246
难免孤独
难免孤独 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 20:11

    This is nothing new, but it's a combination of the best bits of @yangie's approach with a list comprehension, and @EdChum's df.apply approach that I think is easiest to understand.

    First, which columns to we want to pick our values from?

    In [95]: pick_cols = df.apply(pd.Series.first_valid_index, axis=1)
    
    In [96]: pick_cols
    Out[96]: 
    0       A
    1       B
    2       B
    3    None
    dtype: object
    

    Now how do we pick the values?

    In [100]: [df.loc[k, v] if v is not None else None 
        ....:     for k, v in pick_cols.iteritems()]
    Out[100]: [1.0, 3.0, 4.0, None]
    

    This is ok, but we really want the index to match that of the original DataFrame:

    In [98]: pd.Series({k:df.loc[k, v] if v is not None else None
       ....:     for k, v in pick_cols.iteritems()})
    Out[98]: 
    0     1
    1     3
    2     4
    3   NaN
    dtype: float64
    

提交回复
热议问题