0% found this document useful (0 votes)
2 views2 pages

Ex 3 Handling Missing Data Python

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Ex 3 Handling Missing Data Python

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

EX.

NO 3 : Handling Missing Data in Python


Problem
A data analyst is working with a dataset that contains missing values in several
attributes. Missing data can affect the quality of analysis and machine learning
models. Therefore, it is important to identify and handle missing values using
suitable techniques such as removing missing records or replacing them with
statistical values like mean, median, or mode.

Aim
To identify and handle missing values in a dataset using Python.

Procedure
Step 1: Import the required libraries such as pandas and numpy.

Step 2: Load the dataset into Python using the read_csv() function.

Step 3: Display the dataset to understand its structure.

Step 4: Check for missing values using the isnull() function.

Step 5: Calculate the total number of missing values in each column.

Step 6: Replace missing numerical values with mean values.

Step 7: Replace missing categorical values with mode values.

Step 8: Alternatively remove rows or columns containing missing values.

Step 9: Display the cleaned dataset after handling missing values.

Step 10: Interpret the modified dataset.

Python Coding
# Import necessary libraries
import pandas as pd
import numpy as np

# Load dataset
data = pd.read_csv("[Link]")
# Display first few records
print("Original Dataset")
print([Link]())

# Check missing values


print("\nMissing Values in Each Column")
print([Link]().sum())

# Fill missing numerical values with mean


numeric_columns = data.select_dtypes(include=[Link]).columns
data[numeric_columns] = data[numeric_columns].fillna(
data[numeric_columns].mean()
)

# Fill missing categorical values with mode


categorical_columns = data.select_dtypes(exclude=[Link]).columns

for col in categorical_columns:


data[col] = data[col].fillna(data[col].mode()[0])

# Display cleaned dataset


print("\nDataset after Handling Missing Values")
print([Link]())

Result
Thus, missing values in the dataset were successfully identified and handled
using Python techniques.

You might also like