gpt4 book ai didi

python-3.x - pandas.isnull() 不适用于十进制类型?

转载 作者:行者123 更新时间:2023-12-03 11:15:24 25 4
gpt4 key购买 nike

我是不是漏掉了什么或者 pandas.isnull() 有问题?

>>> import pandas as pd
>>> import decimal
>>> d = decimal.Decimal('NaN')
>>> d
Decimal('NaN')
>>> pd.isnull(d)
False
>>> f = float('NaN')
>>> f
nan
>>> pd.isnull(f)
True
>>> pd.isnull(float(d))
True

问题是我有一个带有 decimal.Decimal 值的数据框,并且 df.dropna() 由于这个原因没有删除 NaN...

最佳答案

是的,这不受支持,您可以使用 NaN 不等于自身的属性,它仍然适用于 Decimal 类型:

In [20]:
import pandas as pd
import decimal
d = decimal.Decimal('NaN')
df = pd.DataFrame({'a':[d]})
df

Out[20]:
a
0 NaN

In [21]:
df['a'].apply(lambda x: x != x)

Out[21]:
0 True
Name: a, dtype: bool

所以你可以这样做:

In [26]:
df = pd.DataFrame({'a':[d,1,2,3]})
df[df['a'].apply(lambda x: x == x)]

Out[26]:
a
1 1
2 2
3 3

关于python-3.x - pandas.isnull() 不适用于十进制类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37922331/

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