sequence_hard sequence_hard - 19 days ago 8x
Python Question

How to iterate through rows of a dataframe and check whether value in a column row is NaN

I have a beginner question. I have a dataframe I am iterating over and I want to check if a value in a column2 row is

or not, to perform an action on this value if it is not
. My DataFrame looks like this:


Column1 Column2
0 a hey
1 b NaN
2 c up

What I am trying right now is:

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

The thought behind that is that I iterate over the rows in column 2 and
frame for every row that has a value (which is a string). What I get however is this:

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'

When I only run the first line of my code, I get


which suggests that the floats in the output of the first line are the cause of the error. Can anybody tell me how I can accomplish what I want?


As you already understand , frame in

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

is every row in the Column, its type would be the type of elements in the column (which most probably would not be Series or DataFrame). Hence, frame.notnull() on that would not work.

You should instead try -

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