Iterate through the data frame and select null values

I am trying to iterate using a data frame that has null values ​​for column = [myCol]. I can iterate using a data frame, however, when I specify, I want to see only null values, I get an error.

The ultimate goal is that I want to force a value into the fields that are Null, so I repeat to identify which are the first.

for index,row in df.iterrows(): if(row['myCol'].isnull()): print('true') AttributeError: 'str' object has no attribute 'isnull' 

I tried to specify the column = "No", because this is the value that I see when printing an iteration of the data frame. Not lucky yet:

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

Any help is much appreciated!

+5
source share
1 answer

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

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

But it looks like you need df.fillna(myValue) , where myValue is the value you want to enter in fields that are NULL. And also, to check the NULL fields in the data frame, you can call df.myCol.isnull() instead of looping through the rows and checking individually.


If the columns are of a row type, you might also need to check if it is an empty row:

 for index, row in df.iterrows(): if(row['myCol'] == ""): print('true') 
+2
source

Source: https://habr.com/ru/post/1261731/


All Articles