Python Forum

Full Version: Pandas null values
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I want to iterate through a dataframe and check for a null value:

for i, row in df.iterrows():
   if row['Some column'] is not null:
      Do some stuff!

Thank you
Do you mean None, rather than null?
nan

import numpy as np
df.replace(np.nan, '', regex=True) #this code will replace all the nan (Null) values with an empty string for the entire dataframe

I want to identify a nan value while iterating through rows.
You can use pandas .isnull() or .notnull()
if row.notnull()['Some column']:
 do something
or
if row[['Some column']].notnull():
  do something
(.isnull() or .notnull() are dataframe/series methods, they do not work for single "cell")

You can use check single cell with some function appropriate to a cell type  - like np.isnan() for numerical column or is not None for string field.

Maybe you can avoid iterating with something like
df.isnull().any(axis=1)  # gives True for rows with NaN(s)
combined with .apply().