w3resource

Checking for missing values in a Dataset using Pandas


2. Checking for Missing Values in a Dataset

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

This exercise demonstrates how to check for missing values in a dataset, which is a common pre-processing step in machine learning.

Sample Solution :

Code :

import pandas as pd
# Load the dataset
df = pd.read_csv('data.csv')
# Check for missing values in the dataset
missing_values = df.isna().sum()
# Output the result
print(missing_values)

Output:

ID        0
Name      0
Age       1
Gender    0
Salary    1
Target    0
dtype: int64

Explanation:

  • Loaded the dataset using pd.read_csv().
  • Used isna().sum() to check for missing values in each column.
  • Displayed the number of missing values in each column.

For more Practice: Solve these Related Problems:

  • Write a Pandas program to check for missing values in a dataset and output a summary of missing counts per column.
  • Write a Pandas program to detect columns with over 20% missing values and list those columns.
  • Write a Pandas program to visually represent missing data using a heatmap of the null values.
  • Write a Pandas program to check for missing values in nested data structures within a DataFrame.

Go to:


Python-Pandas Code Editor:

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

Previous: Loading Dataset from CSV.
Next: Pandas - Applying a Custom Function to Rows using apply().

-->

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.