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

Ex.2 - Simple EDA Using Python

The document provides a Python script for handling missing values in a dataset using pandas. It demonstrates various techniques such as dropping rows with missing values, filling missing values with mean or mode, removing duplicates, and outlier removal using the IQR method. Additionally, it includes one-hot encoding for categorical variables like Gender and City.

Uploaded by

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

Ex.2 - Simple EDA Using Python

The document provides a Python script for handling missing values in a dataset using pandas. It demonstrates various techniques such as dropping rows with missing values, filling missing values with mean or mode, removing duplicates, and outlier removal using the IQR method. Additionally, it includes one-hot encoding for categorical variables like Gender and City.

Uploaded by

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

04/03/2026, 07:02 Untitled1.

ipynb - Colab

# Handling the missing values (Python)

import pandas as pd
import numpy as np

data = {
"Age": [21, 25, 32, 42, ""],
"Salary": [70000, [Link], 50000, 70000, 80000],
"Gender": ["female", "male", "female", "male", "male"],
"Education": ["MBA", "MCA", [Link], "Bcom", "MBA"],
"City": ["Chennai", "Bangalore", "Trichy", "Chennai", [Link]]
}

df = [Link](data)
print(df)

# Display first 5 rows


print([Link]())

# Check basic information


print([Link]())

# Check missing values


print([Link]().sum())

# Drop rows with missing values


[Link](inplace=True)

# Fill missing values with 0


[Link](0, inplace=True)

# Missing value imputation


df["Age"].fillna(df["Age"].mean(), inplace=True)
df["City"].fillna(df["City"].mode()[0], inplace=True)

# ------------------------------
# Remove duplicates
df.drop_duplicates(inplace=True)

# ------------------------------
# Outlier removal (IQR Method)

Q1 = df["Salary"].quantile(0.25)
Q3 = df["Salary"].quantile(0.75)
IQR = Q3 - Q1

df = df[
(df["Salary"] >= Q1 - 1.5 * IQR) &
(df["Salary"] <= Q3 + 1.5 * IQR)
]

# ------------------------------
# Encoding (One-Hot Encoding)

df = pd.get_dummies(df, columns=["Gender", "City"], drop_first=True)

print(df)

Age Salary Gender Education City


0 21 70000.0 female MBA Chennai
1 25 NaN male MCA Bangalore
2 32 50000.0 female NaN Trichy
3 42 70000.0 male Bcom Chennai
4 80000.0 male MBA NaN

[Link] 1/2
04/03/2026, 07:02 [Link] - Colab
Age Salary Gender Education City
0 21 70000.0 female MBA Chennai
1 25 NaN male MCA Bangalore
2 32 50000.0 female NaN Trichy
3 42 70000.0 male Bcom Chennai
4 80000.0 male MBA NaN
<class '[Link]'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 5 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Age 5 non-null object
1 Salary 4 non-null float64
2 Gender 5 non-null object
3 Education 4 non-null object
4 City 4 non-null object
dtypes: float64(1), object(4)
memory usage: 332.0+ bytes
None
Age 0
Salary 1
Gender 0
Education 1
City 1
dtype: int64
Age Salary Education Gender_male
0 21 70000.0 MBA False
3 42 70000.0 Bcom True
/tmp/ipykernel_903/[Link]: FutureWarning: A value is trying to be set on a copy of a Dat
The behavior will change in pandas 3.0. This inplace method will never work because the intermedia

For example, when doing 'df[col].method(value, inplace=True)', try using '[Link]({col: value},

df["Age"].fillna(df["Age"].mean(), inplace=True)
/tmp/ipykernel_903/[Link]: FutureWarning: A value is trying to be set on a copy of a Dat
The behavior will change in pandas 3.0. This inplace method will never work because the intermedia

For example, when doing 'df[col].method(value, inplace=True)', try using '[Link]({col: value},

df["City"].fillna(df["City"].mode()[0], inplace=True)

Start coding or generate with AI.

[Link] 2/2

You might also like