Python Forum
Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pandas null values
#1
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
Reply
#2
Do you mean None, rather than null?
Reply
#3
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.
Reply
#4
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().
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Assigning conditional values in Pandas Scott 3 795 Dec-19-2023, 03:10 AM
Last Post: Larz60+
  pandas: Compute the % of the unique values in a column JaneTan 1 1,777 Oct-25-2021, 07:55 PM
Last Post: jefsummers
  update values in one dataframe based on another dataframe - Pandas iliasb 2 9,253 Aug-14-2021, 12:38 PM
Last Post: jefsummers
  Pandas + Groupby + Filter unique values JosepMaria 1 2,873 Jun-15-2020, 08:15 AM
Last Post: JosepMaria
  Pandas copying wrong values vmarg 2 2,388 Jan-06-2020, 09:45 AM
Last Post: vmarg
  sort values of a column pandas karlito 2 2,497 Oct-22-2019, 06:11 AM
Last Post: karlito
  Pandas Import CSV count between numerical values within 1 Column ptaylor520 3 2,649 Jul-16-2019, 08:13 AM
Last Post: ptaylor520
  Custom timeinterval converted to hourly values using Pandas? SinPy 1 2,782 Jun-07-2019, 05:06 AM
Last Post: heiner55
  Splitting values in column in a pandas dataframe based on a condition hey_arnold 1 4,168 Jul-24-2018, 02:18 PM
Last Post: hey_arnold

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020