Dropping rows with missing values using Pandas
Pandas: Machine Learning Integration Exercise-3 with Solution
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.
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.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics