Pandas DataFrame Replace NaT with None

前端 未结 4 957
南笙
南笙 2020-12-14 08:55

I have been struggling with this question for a long while, and I tried different methods.

I have a simple DataFrame as shown,

I can use code to rep

4条回答
  •  攒了一身酷
    2020-12-14 09:34

    Make the column type as str first

     dfTest2.InvoiceDate =  dfTest2.InvoiceDate.astype(str)
    

    then compare it directly with "NaT" and replace with None

    dfTest2.InvoiceDate = dfTest2.InvoiceDate.apply(lambda x : None if x=="NaT" else x)
    

提交回复
热议问题