How set values in pandas dataframe based on NaN values of another column?

前端 未结 1 1651
日久生厌
日久生厌 2021-01-25 19:23

I have dataframe named df with original shape (4361, 15). Some of agefm column`s values are NaN. Just look:

> df[df.a         


        
相关标签:
1条回答
  • 2021-01-25 19:52

    The best is use numpy.where:

    df['nevermarr'] = np.where(df.agefm.isnull(), 1, 0)
    print (df)
       agefm  nevermarr
    0    NaN          1
    1    5.0          0
    2    6.0          0
    

    Or use loc, ==True can be omitted:

    df.loc[df.agefm.isnull(), 'nevermarr'] = 1
    

    Or mask:

    df['nevermarr'] = df.nevermarr.mask(df.agefm.isnull(), 1)
    print (df)
       agefm  nevermarr
    0    NaN          1
    1    5.0          2
    2    6.0          3
    

    Sample:

    import pandas as pd
    import numpy as np
    
    df = pd.DataFrame({'nevermarr':[7,2,3],
                       'agefm':[np.nan,5,6]})
    
    print (df)
       agefm  nevermarr
    0    NaN          7
    1    5.0          2
    2    6.0          3
    
    df.loc[df.agefm.isnull(), 'nevermarr'] = 1
    print (df)
       agefm  nevermarr
    0    NaN          1
    1    5.0          2
    2    6.0          3
    
    0 讨论(0)
提交回复
热议问题