w3resource

Checking for Missing Values in a Pandas DataFrame


1. Checking Missing Values in a DataFrame

Write a Pandas program to check for missing values in a DataFrame.

This exercise demonstrates how to check for missing values in a DataFrame using isna() and any().

Sample Solution :

Code :

import pandas as pd

# Create a sample DataFrame with missing values
df = pd.DataFrame({
    'Name': ['Orville', 'Arturo', 'Ruth', None],
    'Age': [25, 30, None, 22],
    'Salary': [50000, None, 70000, 60000]
})

# Check if there are any missing values in the DataFrame
missing_values = df.isna().any()

# Output the result
print(missing_values)

Output:

Name      True
Age       True
Salary    True
dtype: bool

Explanation:

  • Created a DataFrame with missing values.
  • Used isna() to check for missing values and any() to determine if any column contains missing values.
  • Displayed the result, which shows True for columns with missing data.

For more Practice: Solve these Related Problems:

  • Write a Pandas program to check for missing values in a DataFrame and output the percentage of missing values per column.
  • Write a Pandas program to identify columns with missing values and list the row indices where they occur.
  • Write a Pandas program to check for missing values in a DataFrame and generate a summary report with the count and percentage for each column.
  • Write a Pandas program to check for missing values and create a new DataFrame that only includes rows without any missing data.

Python-Pandas Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.