Filtering all rows with NaT in a column in Dataframe python

后端 未结 4 1397
有刺的猬
有刺的猬 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:41

    For those interested, in my case I wanted to drop the NaT contained in the DateTimeIndex of a dataframe. I could not directly use the notnull construction as suggested by Karl D. You first have to create a temporary column out of the index, then apply the mask, and then delete the temporary column again.

    df["TMP"] = df.index.values                # index is a DateTimeIndex
    df = df[df.TMP.notnull()]                  # remove all NaT values
    df.drop(["TMP"], axis=1, inplace=True)     # delete TMP again
    

提交回复
热议问题