Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Data preprocessing
  • Data Manipulation
  • Data Analysis using Pandas
  • EDA
  • Pandas Exercise
  • Pandas AI
  • Numpy
  • Matplotlib
  • Plotly
  • Data Analysis
  • Machine Learning
  • Data science
Open In App
Next Article:
Check for True or False in Python
Next article icon

Check For NaN Values in Python

Last Updated : 21 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In data analysis and machine learning, missing or NaN (Not a Number) values can often lead to inaccurate results or errors. Identifying and handling these NaN values is crucial for data preprocessing. Here are five methods to check for NaN values in Python.

What are Nan Values In In Python?

In Python, NaN stands for "Not a Number". It is a special floating-point value defined in the IEEE 754 floating-point standard, used to represent undefined or unrepresentable numerical results. NaN values are commonly encountered in data science and numerical computing when working with datasets that contain missing, undefined, or invalid values.

Methods To Check For NaN Values

Using isnan() from NumPy

The numpy.isnan() function is a simple way to check for NaN values in a NumPy array.

Python
# code import numpy as np  # Example array array = np.array([1, 2, np.nan, 4, 5])  # Check for NaN values nan_check = np.isnan(array) print(nan_check) 

Output:

[False False  True False False]

Using isnull() from Pandas

Pandas provides the isnull() function, which is useful for checking NaN values in DataFrames and Series.

Python
# code import pandas as pd  # Example DataFrame data = {'A': [1, 2, np.nan, 4], 'B': [5, np.nan, 7, 8]} df = pd.DataFrame(data)  # Check for NaN values nan_check_df = df.isnull() print(nan_check_df) 

Output:

       A      B
0 False False
1 False True
2 True False
3 False False

Using isna() from Pandas

The isna() function in Pandas is an alias for isnull(). It works in the same way.

Python
# code # Check for NaN values nan_check_df = df.isna() print(nan_check_df) 

Output:

       A      B
0 False False
1 False True
2 True False
3 False False

Using pd.isna()

Pandas also provides a top-level pd.isna() function, which can be used on both DataFrames and Series.

Python
# code # Check for NaN values in a Series nan_check_series = pd.isna(df['A']) print(nan_check_series) 

Output:

0    False
1 False
2 True
3 False
Name: A, dtype: bool

Conclusion

These methods provide a robust toolkit for identifying NaN values in Python. Whether you are working with NumPy arrays or Pandas DataFrames, you can efficiently check for missing values and take appropriate actions to handle them. This step is crucial in ensuring the accuracy and reliability of your data analysis and machine learning models.



Next Article
Check for True or False in Python

M

monuro08eb
Improve
Article Tags :
  • Python
  • Python-pandas
Practice Tags :
  • python

Similar Reads

  • Check If Value Is Int or Float in Python
    In Python, you might want to see if a number is a whole number (integer) or a decimal (float). Python has built-in functions to make this easy. There are simple ones like type() and more advanced ones like isinstance(). In this article, we'll explore different ways to check if a value is an integer
    4 min read
  • Check for True or False in Python
    Python has built-in data types True and False. These boolean values are used to represent truth and false in logical operations, conditional statements, and expressions. In this article, we will see how we can check the value of an expression in Python. Common Ways to Check for True or FalsePython p
    2 min read
  • Check if the value is infinity or NaN in Python
    In this article, we will check whether the given value is NaN or Infinity. This can be done using the math module. Let's see how to check each value in detail. Check for NaN values in Python NaN Stands for "Not a Number" and it is a numeric datatype used as a proxy for values that are either mathema
    4 min read
  • Python | Check if all values in numpy are zero
    Given a numpy array, the task is to check whether the numpy array contains all zeroes or not. Let's discuss few ways to solve the above task. Method #1: Getting count of Zeros using numpy.count_nonzero() C/C++ Code # Python code to demonstrate # to count the number of elements # in numpy which are z
    3 min read
  • Check for NaN in Pandas DataFrame
    NaN stands for Not A Number and is one of the common ways to represent the missing value in the data. It is a special floating-point value and cannot be converted to any other type than float.  NaN value is one of the major problems in Data Analysis. It is very essential to deal with NaN in order to
    3 min read
  • Python | Pandas DataFrame.values
    Pandas DataFrame is a two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). Arithmetic operations align on both row and column labels. It can be thought of as a dict-like container for Series objects. This is the primary data structure o
    2 min read
  • Ways to Create NaN Values in Pandas DataFrame
    Let's discuss ways of creating NaN values in the Pandas Dataframe. There are various ways to create NaN values in Pandas dataFrame. Those are: Using NumPy Importing csv file having blank values Applying to_numeric function Method 1: Using NumPy [GFGTABS] Python3 import pandas as pd import numpy as n
    1 min read
  • Python - Check for float string
    Checking for float string refers to determining whether a given string can represent a floating-point number. A float string is a string that, when parsed, represents a valid float value, such as "3.14", "-2.0", or "0.001". For example: "3.14" is a float string."abc" is not a float string.Using try-
    2 min read
  • Python - cmath.isnan() function
    cMath module contains a number of functions which is used for mathematical operations for complex numbers. The cmath.isnan() function is used to check whether the value is nan (Not a Number), or not. The value passed in this function can be int, float, and complex numbers. Syntax: cmath.isnan(x) Par
    1 min read
  • Python | Pandas dataframe.first_valid_index()
    Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas dataframe.first_valid_index() function returns index for first non-NA/null valu
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences