Querying for NaN and other names in Pandas

后端 未结 7 1558
生来不讨喜
生来不讨喜 2020-12-12 20:50

Say I have a dataframe df with a column value holding some float values and some NaN. How can I get the part of the dataframe where we

相关标签:
7条回答
  • 2020-12-12 21:15

    You can use the isna and notna Series methods, which is concise and readable.

    import pandas as pd
    import numpy as np
    
    df = pd.DataFrame({'value': [3, 4, 9, 10, 11, np.nan, 12]})
    available = df.query("value.notna()")
    print(available)
    
    #    value
    # 0    3.0
    # 1    4.0
    # 2    9.0
    # 3   10.0
    # 4   11.0
    # 6   12.0
    
    not_available = df.query("value.isna()")
    print(not_available)
    
    #    value
    # 5    NaN
    

    Alternatively, you can use the toplevel pd.isna function, by referencing it as a local variable.

    import pandas as pd
    import numpy as np
    
    
    df = pd.DataFrame({'value': [3, 4, 9, 10, 11, np.nan, 12]})
    df.query("@pd.isna(value)")
    
    #    value
    # 5    NaN
    
    0 讨论(0)
提交回复
热议问题