This tutorial will look at how to get the columns with missing values in a pandas dataframe.
How to find columns with missing values in a pandas dataframe?
To get the columns containing missing values, you can use a combination of the pandas isna()
function and the any()
function in Python.
The idea is to find the columns containing any missing values. The following is the syntax –
# get names of columns with missing values df.columns[df.isna().any()]
Here, we first, create a boolean index of columns with missing values and use it to filter df.columns
to get the columns that contain any missing values.
Examples
Let’s now look at some examples of using the above syntax –
First, we will create a pandas dataframe.
import numpy as np import pandas as pd # employee data data = { "Name": ["Jim", "Dwight", "Angela", "Tobi"], "Age": [26, np.nan, 27, 32], "Department": ["Sales", "Sales", None, np.nan] } # create pandas dataframe df = pd.DataFrame(data) # display the dataframe df
Output:
Here, we created a dataframe with information about some employees in an office. The dataframe has the columns – “Name”, “Age”, and “Department”. You can see that some of the values in the above dataframe are missing.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Let’s now get the columns in the above dataframe containing missing values.
We can use the pandas dataframe isna()
function to check whether a value in a dataframe is a missing value or not.
Let’s apply this function to the above dataframe.
df.isna()
Output:
We get a dataframe with boolean values. Each cell here represents whether the value in this position (in the original dataframe) is missing (na) or not.
Now, we want columns that contain any missing values. You can apply the any()
function on top of the resulting dataframe from the isna()
function.
df.isna().any()
Output:
Name False Age True Department True dtype: bool
We get a boolean series with the columns containing any missing values marked at True
.
You can use the above result as a boolean index to filter the df.columns
and get the names of the columns with missing values.
df.columns[df.isna().any()]
Output:
Index(['Age', 'Department'], dtype='object')
We get the names of the columns with missing values (“Age” and “Department”) in the above dataframe.
You may also be interested in –
- Pandas – Count Missing Values in Each Column
- Pandas – fillna with values from another column
- Drop Rows with NaNs in Pandas DataFrame
- Pandas – Drop Duplicate Columns From Dataframe
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.