Filtering all rows with NaT in a column in Dataframe python

后端 未结 4 1384
有刺的猬
有刺的猬 2020-12-28 13:01

I have a df like this:

    a b           c
    1 NaT         w
    2 2014-02-01  g
    3 NaT         x   

    df=df[df.b==\'2014-02-01\']

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-28 13:21

    Using your example dataframe:

    df = pd.DataFrame({"a":[1,2,3], 
                       "b":[pd.NaT, pd.to_datetime("2014-02-01"), pd.NaT], 
                       "c":["w", "g", "x"]})
    

    Until v0.17 this didn't use to work:

    df.query('b != b') 
    

    and you had to do:

    df.query('b == "NaT"') # yes, surprisingly, this works!
    

    Since v0.17 though, both methods work, although I would only recommend the first one.

提交回复
热议问题