gpt4 book ai didi

python - 错误: 'float' object has no attribute 'isna' "

转载 作者:行者123 更新时间:2023-12-01 00:18:35 25 4
gpt4 key购买 nike

我有以下系列:

my_series = pd.Series([np.nan, np.nan, ['A', 'B']])

我必须循环遍历 my_series 并评估该值是否为 NaN,然后​​执行某些操作(为了简单起见,将操作定义为“do A”和“do B”)。

第一次尝试:

for element in my_series:
if element.isna():
print('do A')
else:
print('do B')

运行时出现错误:“'float'对象没有属性'isna'”

第二次尝试来自问题:Error: float object has no attribute notnull

for element in my_series:
np.where(element.isnull(), 'do A', 'do B')

运行时,出现错误:“AttributeError: 'float' 对象没有属性 'isnull'

我在 StackOverflow 上没有找到任何其他类似的问题,我不知道还能尝试什么。

最佳答案

将代码更改为:

for element in my_series:
if type(element) == float and pd.isna(element):
print('do A')
else:
print('do B')

根据Peter的评论进行编辑

我故意不改变最初处理源码的概念系列循环。看起来两个打印指令都是“占位符”,替换为一段用于 NaN 值的代码和另一段用于其他值的代码。

关于python - 错误: 'float' object has no attribute 'isna' ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59118039/

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