w3resource

Dropping rows with missing values using Pandas


3. Dropping Rows with Missing Values from a Dataset

Write a Pandas program to drop rows with missing values from a dataset.

This exercise demonstrates how to remove rows with missing values from the dataset using dropna().

Sample Solution :

Code :

import pandas as pd

# Load the dataset
df = pd.read_csv('data.csv')

# Drop rows with missing values
df_cleaned = df.dropna()

# Output the cleaned dataset
print(df_cleaned)

Output:

   ID      Name   Age  Gender   Salary  Target
0   1      Sara  25.0  Female  50000.0       0
1   2    Ophrah  30.0    Male  60000.0       1
2   3    Torben  22.0    Male  70000.0       0
3   4  Masaharu  35.0    Male  80000.0       1 

Explanation:

  • Loaded the dataset using Pandas.
  • Used dropna() to remove rows that contain any missing values.
  • Displayed the cleaned dataset.

For more Practice: Solve these Related Problems:

  • Write a Pandas program to drop rows with missing values only in specific columns and report the change in DataFrame size.
  • Write a Pandas program to drop rows with missing values and then reset the index of the resulting DataFrame.
  • Write a Pandas program to drop rows that have more than a specified number of missing entries.
  • Write a Pandas program to drop rows with missing values and compare summary statistics before and after the operation.

Go to:


Python-Pandas Code Editor:

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

Previous: Checking for Missing Values in a Dataset.
Next: Filling Missing Values with the Mean.

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.