我已经为这个问题苦苦挣扎了很长时间,并且尝试了不同的方法。
我有一个简单的DataFrame如图所示,
我可以使用代码将NaN
替换为None
(而不是字符串“None”),[![dfTest2 = dfTest.where(pd.notnull(dfTest), None)][2]][2]
我支持NaT
也归类为“Null”,因为以下情况:
但是,NaT
不会替换为None
。
我一直在寻找答案,但没有运气。有人可以帮忙吗?
先感谢您。
最佳答案
制作dtype
object
dfTest2 = pd.DataFrame(dict(InvoiceDate=pd.to_datetime(['2017-06-01', pd.NaT])))
dfTest2.InvoiceDate.astype(object).where(dfTest2.InvoiceDate.notnull(), None)
0 2017-06-01 00:00:00
1 None
Name: InvoiceDate, dtype: object
关于python-2.7 - Pandas DataFrame用无替换NaT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42818262/