Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate through dataframe and select null values

I am trying to iterate through a dataframe that has null values for the column = [myCol]. I am able to iterate through the dataframe fine, however when I specify I only want to see null values I get an error.

End goal is that I want to force a value into the fields that are Null which is why I am iterating to identify which are first.

for index,row in df.iterrows():
    if(row['myCol'].isnull()):
        print('true')

AttributeError: 'str' object has no attribute 'isnull'

I tried specifying the column = 'None' since that is the value I see when I print the iteration of the dataframe. Still no luck:

for index,row in df.iterrows():
    if(row['myCol']=='None'):
        print('true')

No returned rows

Any help greatly appreciated!

like image 836
Ariel Avatar asked Dec 22 '16 16:12

Ariel


1 Answers

You can use pd.isnull() to check if a value is null or not:

for index, row in df.iterrows():
    if(pd.isnull(row['myCol'])):
        print('true')

But seems like you need df.fillna(myValue) where myValue is the value you want to force into fields that are NULL. And also to check the NULL fields in a data frame you can invoke df.myCol.isnull() instead of looping through rows and check individually.


If the columns are of string type, you might also want check if it is empty string:

for index, row in df.iterrows():
    if(row['myCol'] == ""):
        print('true')
like image 50
Psidom Avatar answered Nov 12 '22 20:11

Psidom