Untitled
Anonymous
plain_text
02/19/2026 6:21 AM
3.8 KB
12
Indexable
Titanic Dataset preprocessing Overview
The titanic passenger dataset cotains real word data collected from multiple sources and includes several common data quality issue such as missing value , irrelevent attributes, inconsistent data type, duplicate records and extreme value (outliers).
Before performing any statistical analysis or building predictive models, this dataset must be properly cleaned and prepared.
LAB OBJECTIVE
In this lab, you are required to use python and the pandas library to preprocess the titanic dataset by performing the following tasks:
1 loading and inspacting the dataset structure
2.identifying and handling missing values in key attributes such as Age and Embarked
3. removing cloumns that contain excessive missing information
4. detecting and removing duplicate records
5. connection appropriate columns to correct data type
6. identifying and treating outliers in numarical form
7. encoding categorical variables into numarical forms
----------------------------------------------------------------------------------------------------------------------
import pandas as pd
import numpy as np
import matplotlib.pyplot as pit
import seaborn as sns
------------------------------------------
df=sns.load_dataset("titanic")
----------------------------
df.head()
----------
df.tail()
--------
df.shape
-----------
df.isnull().sum()
-----------------
df.info()
--------
df.drop(columns=["deck"],inplace=True)
------------------------------------
df.head()
------
duplicates = df.duplicated()
duplicates
----------
duplicates =df.duplicated()
duplicate
--------------------------
df.isnull().sum().sort_values(ascending = False)
------------------------------------------------
df['age'].fillna(df['age'].median(),inplace=True)
-----------------------------------------------------
df.head()
---------
print("Mean Age", df['age'].mean())
-----------------------------------
print("Median age",df['age'].median())
-----------------------------------------
import seaborn as sns
import matplotlib.pyplot as plt
-------------------------------------------
sns.histplot(df['age'],kde=True)
plt.title("Age Distribution")
plt.show()
------------------------------------------
df['age'].skew()
np.float64(0.5102446555756495)
---------------------------------------------
df['embarked'].fillna(df['embarked'].mode()[0],inplace = True)
------------------------------------------------------------------
df['embark_town'].fillna(df['embark_town'].mode()[0],inplace = True)
---------------------------------------------------------------------
df['embarked'].value_counts()
---------------------------------------------------------------------
df['embark_town'].value_counts()
---------------------------------
df.isnull().sum()
----------------
df[['embarked','embark_town']].isnull().sum()
------------------------------------------------
Editor is loading...
Leave a Comment