我有一个初学者问题。我有一个正在迭代的数据框,我想检查 column2 行中的值是否为 NaN
,如果不是 NaN
则对该值执行操作>。我的数据框看起来像这样:
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 列中的行和 print
帧中的每一行进行迭代,以获取具有值(字符串)的每一行。然而,我得到的是:
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
这表明第一行输出中的 float 是错误的原因。任何人都可以告诉我如何才能完成我想要的吗?
最佳答案
如您所知,frame
在
for item, frame in df['Column2'].iteritems():
是列中的每个 行
,它的类型是列中元素的类型(很可能不是 Series
或 DataFrame
)。因此,frame.notnull()
将不起作用。
你应该试试——
for item, frame in df['Column2'].iteritems():
if pd.notnull(frame):
print frame
关于python - 如何遍历数据帧的行并检查列行中的值是否为 NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33124117/