Querying for NaN and other names in Pandas

后端 未结 7 1575
生来不讨喜
生来不讨喜 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:12

    In general, you could use @local_variable_name, so something like

    >>> pi = np.pi; nan = np.nan
    >>> df = pd.DataFrame({"value": [3,4,9,10,11,np.nan,12]})
    >>> df.query("(value < 10) and (value > @pi)")
       value
    1      4
    2      9
    

    would work, but nan isn't equal to itself, so value == NaN will always be false. One way to hack around this is to use that fact, and use value != value as an isnan check. We have

    >>> df.query("(value < 10) or (value == @nan)")
       value
    0      3
    1      4
    2      9
    

    but

    >>> df.query("(value < 10) or (value != value)")
       value
    0      3
    1      4
    2      9
    5    NaN
    

提交回复
热议问题