- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python - How to Count the NaN Occurrences in a Column in Pandas Dataframe?
To count the NaN occurrences in a column, use the isna(). Use the sum() to add the values and find the count.
At first, let us import the required libraries with their respective aliases −
import pandas as pd import numpy as np
Create a DataFrame. We have set the NaN values using the Numpy np.inf in “Units_Sold” column −
dataFrame = pd.DataFrame({"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'],"Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000],"Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000],"Units_Sold": [ 100, np.NaN, 150, np.NaN, 200, np.NaN] })
Count NaN values from column "Units_Sold" −
dataFrame["Units_Sold"].isna().sum()
Example
Following is the code −
import pandas as pd import numpy as np # creating dataframe dataFrame = pd.DataFrame({"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'],"Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000],"Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000],"Units_Sold": [ 100, np.NaN, 150, np.NaN, 200, np.NaN] }) print("Dataframe...\n",dataFrame) # count NaN values from column "Units_Sol" count = dataFrame["Units_Sold"].isna().sum() print("\nCount of NaN values in column Units_Sold...\n",count)
Output
This will produce the following output −
Dataframe... Car Cubic_Capacity Reg_Price Units_Sold 0 BMW 2000 7000 100.0 1 Lexus 1800 1500 NaN 2 Tesla 1500 5000 150.0 3 Mustang 2500 8000 NaN 4 Mercedes 2200 9000 200.0 5 Jaguar 3000 6000 NaN Count of NaN values in column Units_Sold... 3
- Related Articles
- How to count the NaN values in a column in a Python Pandas DataFrame?
- How to replace NaN values by Zeroes in a column of a Pandas DataFrame?
- Python - Calculate the count of column values of a Pandas DataFrame
- Merge Python Pandas dataframe with a common column and set NaN for unmatched values
- Count the frequency of a value in a DataFrame column in Pandas
- Python Pandas - Replace all NaN elements in a DataFrame with 0s
- How to check if any value is NaN in a Pandas DataFrame?
- Apply uppercase to a column in Pandas dataframe in Python
- Python - How to select a column from a Pandas DataFrame
- Python - Move a column to the first position in Pandas DataFrame?
- How to shift a column in a Pandas DataFrame?
- Python Pandas – Count the rows and columns in a DataFrame
- Python - Add a zero column to Pandas DataFrame
- Python – Create a new column in a Pandas dataframe
- Merge Pandas dataframe with a common column and set NaN for unmatched values

Advertisements