w3resource

Checking for Missing Values in a Pandas DataFrame


Pandas: Data Validation Exercise-1 with Solution


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.

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.