Goal
Perform EDA on the Titanic dataset and produce:
- Data quality findings (missing values, types)
- A handful of clear plots
- Insights about survival patterns
Analysis pipeline
Every EDA project follows the same rhythm: load the raw file, fix the messy parts, ask simple questions of the clean data, draw a picture, then write down what you saw.
flowchart LR A["Raw CSV
(Titanic passengers)"] --> B["Clean
(fill Embarked, flag Cabin)"] B --> C["Explore
(value_counts, groupby)"] C --> D["Visualize
(survival by sex/class/fare)"] D --> E["Conclude
(who survived, and why)"]
Dataset
Common sources:
- Kaggle: Titanic - Machine Learning from Disaster
Typical columns:
Survived,Pclass,Sex,Age,SibSp,Parch,Fare,Embarked
Step 1: Load data
import pandas as pd
df = pd.read_csv("data/titanic.csv")
print(df.shape)
print(df.head())Step 2: Schema and missingness
print(df.info())
missing = (df.isna().mean() * 100).sort_values(ascending=False)
print(missing)Focus on missing in:
AgeCabinEmbarked
Step 3: Clean minimal issues
Handle missing Embarked (small)
if "Embarked" in df.columns:
df["Embarked"] = df["Embarked"].fillna(df["Embarked"].mode().iloc[0])Keep Cabin as “has cabin” flag
if "Cabin" in df.columns:
df["has_cabin"] = df["Cabin"].notna()Step 4: Univariate plots
Survival distribution
import seaborn as sns
import matplotlib.pyplot as plt
plt.figure(figsize=(6, 4))
sns.countplot(data=df, x="Survived")
plt.title("Survival counts")
plt.tight_layout()
plt.show()Age distribution
import seaborn as sns
import matplotlib.pyplot as plt
if "Age" in df.columns:
plt.figure(figsize=(7, 4))
sns.histplot(df["Age"].dropna(), bins=30, kde=True)
plt.title("Age distribution")
plt.tight_layout()
plt.show()Step 5: Survival by category
Survival by sex
import seaborn as sns
import matplotlib.pyplot as plt
plt.figure(figsize=(7, 4))
sns.barplot(data=df, x="Sex", y="Survived")
plt.title("Survival rate by sex")
plt.tight_layout()
plt.show()Survival by passenger class
import seaborn as sns
import matplotlib.pyplot as plt
plt.figure(figsize=(7, 4))
sns.barplot(data=df, x="Pclass", y="Survived")
plt.title("Survival rate by class")
plt.tight_layout()
plt.show()Step 6: Numeric relationships
Fare vs survival (boxplot)
import seaborn as sns
import matplotlib.pyplot as plt
plt.figure(figsize=(7, 4))
sns.boxplot(data=df, x="Survived", y="Fare")
plt.title("Fare vs survival")
plt.tight_layout()
plt.show()Visualize it
A pivot_table collapses "survival by sex" and "survival by class" into a single
matrix — the same trick McKinney uses to get mean movie ratings by gender in one
line instead of several separate groupby calls.
matrix = df.pivot_table("Survived", index="Pclass", columns="Sex", aggfunc="mean")
print(matrix)Step 7: Write insights (example)
Write 5–10 bullet insights such as:
- Survival rate is higher for females.
- Higher class passengers survived more.
- Passengers who paid higher fare tended to survive more.
- Missingness is high in Cabin; treat as a feature (“has_cabin”).
Deliverable
Save a cleaned dataset version:
df.to_csv("output/titanic_cleaned.csv", index=False)Check your understanding
Cabin is missing for most passengers. What does the project do instead of dropping the column?
The missingness itself is a signal, so it is kept as a boolean feature.
Show answer
A — Creates a has_cabin flag from Cabin.notna() — The missingness itself is a signal, so it is kept as a boolean feature.
How is the small number of missing Embarked values filled?
Embarked is categorical and nearly complete, so the most frequent value is a reasonable fill.
Show answer
A — With the mode (most common port) — Embarked is categorical and nearly complete, so the most frequent value is a reasonable fill.
What does sns.barplot(data=df, x="Sex", y="Survived") show, given Survived is 0 or 1?
barplot averages Survived per group, and the mean of a 0/1 column is the survival rate.
Show answer
A — The survival rate for each sex — barplot averages Survived per group, and the mean of a 0/1 column is the survival rate.
What does df.pivot_table("Survived", index="Pclass", columns="Sex", aggfunc="mean") produce?
It collapses survival by sex and survival by class into a single matrix.
Show answer
A — A class by sex matrix of survival rates — It collapses survival by sex and survival by class into a single matrix.
🧪 Try It Yourself
Exercise 1 – Overall survival rate
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 – Survival rate by sex
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 – Missing value check
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.