微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python – 如何遍历数据帧的行并检查列行中的值是否为NaN

我有一个初学者的问题.我有一个迭代的数据帧,我想检查column2行中的值是否为NaN,如果它不是NaN,则对该值执行操作.我的DataFrame看起来像这样:

df:

  Column1  Column2
0    a        hey
1    b        NaN
2    c        up

我现在正在尝试的是:

for item, frame in df['Column2'].iteritems():
    if frame.notnull() == True:
        print 'frame'

这背后的想法是我遍历第2列中的行并为具有值(字符串)的每一行打印帧.然而,我得到的是:

AttributeError                            Traceback (most recent call last)
<ipython-input-80-8b871a452417> in <module>()
      1 for item, frame in df['Column2'].iteritems():
----> 2     if frame.notnull() == True:
      3         print 'frame'

AttributeError: 'float' object has no attribute 'notnull'

当我只运行我的代码的第一行时,我得到了

0
hey
1
nan
2
up

这表明第一行输出中的浮点数是导致错误的原因.任何人都可以告诉我如何实现我的目标吗?

解决方法:

如你所知,框架

for item, frame in df['Column2'].iteritems():

是Column中的每一行,它的类型将是列中元素的类型(很可能不是Series或DataFrame).因此,frame.notnull()就不行了.

你应该尝试 –

for item, frame in df['Column2'].iteritems():
    if pd.notnull(frame):
        print frame

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐