w3resource

Checking for missing values in a Dataset using Pandas


Pandas: Machine Learning Integration Exercise-2 with Solution


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.

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.