gpt4 book ai didi

python-2.7 - Pandas DataFrame用无替换NaT

转载 作者:行者123 更新时间:2023-12-03 23:31:47 33 4
gpt4 key购买 nike

我已经为这个问题苦苦挣扎了很长时间,并且尝试了不同的方法。

我有一个简单的DataFrame如图所示,

enter image description here

我可以使用代码将NaN替换为None(而不是字符串“None”),
[![dfTest2 = dfTest.where(pd.notnull(dfTest), None)][2]][2]
enter image description here

我支持NaT也归类为“Null”,因为以下情况:
enter image description here

但是,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/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com