sequence_hard sequence_hard - 2 months ago 13
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

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

df:

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
print
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

0
hey
1
nan
2
up


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?

Answer

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