Pandas - find first non-null value in column

前端 未结 3 1428
清酒与你
清酒与你 2020-12-14 06:39

If I have a series that has either NULL or some non-null value. How can I find the 1st row where the value is not NULL so I can report back the datatype to the user. If th

3条回答
  •  庸人自扰
    2020-12-14 07:20

    For a series this will return the first no null value:

    Creating Series s:

    s = pd.Series(index=[2,4,5,6], data=[None, None, 2, None])
    

    which creates this Series:

    2    NaN
    4    NaN
    5    2.0
    6    NaN
    dtype: float64
    

    You can get the first non-NaN value by using:

    s.loc[~s.isnull()].iloc[0]
    

    which returns

    2.0
    

    If you on the other hand have a dataframe like this one:

    df = pd.DataFrame(index=[2,4,5,6], data=np.asarray([[None, None, 2, None], [1, None, 3, 4]]).transpose(), 
                      columns=['a', 'b'])
    

    which looks like this:

        a       b
    2   None    1
    4   None    None
    5   2       3
    6   None    4
    

    you can select per column the first non null value using this (for column a):

    df.a.loc[~df.a.isnull()].iloc[0]
    

    or if you want the first row containing no Null values anywhere you can use:

    df.loc[~df.isnull().sum(1).astype(bool)].iloc[0]
    

    Which returns:

    a    2
    b    3
    Name: 5, dtype: object
    

提交回复
热议问题