Python Central HubSign in

Data Analytics with Python / Phase 10: Data Analytics Projects

Exploratory Data Analysis (EDA) on Titanic

A guided end-to-end EDA project - load data, clean issues, visualize relationships, and summarize insights.

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.

diagram Titanic EDA pipeline mermaid
From raw CSV to survival insights, one stage at a time.

Dataset

Common sources:

  • Kaggle: Titanic - Machine Learning from Disaster

Typical columns:

  • Survived, Pclass, Sex, Age, SibSp, Parch, Fare, Embarked

Step 1: Load data

Python
import pandas as pd
 
df = pd.read_csv("data/titanic.csv")
print(df.shape)
print(df.head())

Step 2: Schema and missingness

Python
print(df.info())
 
missing = (df.isna().mean() * 100).sort_values(ascending=False)
print(missing)

Focus on missing in:

  • Age
  • Cabin
  • Embarked

Step 3: Clean minimal issues

Handle missing Embarked (small)

Python
if "Embarked" in df.columns:
    df["Embarked"] = df["Embarked"].fillna(df["Embarked"].mode().iloc[0])

Keep Cabin as “has cabin” flag

Python
if "Cabin" in df.columns:
    df["has_cabin"] = df["Cabin"].notna()

Step 4: Univariate plots

Survival distribution

Python
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

Python
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

Python
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

Python
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)

Python
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.

Python
matrix = df.pivot_table("Survived", index="Pclass", columns="Sex", aggfunc="mean")
print(matrix)
sketch Survival rate by class and sex p5.js
Taller amber bars mean a higher survival rate for that class/sex group.

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:

Python
df.to_csv("output/titanic_cleaned.csv", index=False)

Check your understanding

quizCheck yourself
  1. Cabin is missing for most passengers. What does the project do instead of dropping the column?

    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.

  2. How is the small number of missing Embarked values filled?

    Show answer

    A — With the mode (most common port) — Embarked is categorical and nearly complete, so the most frequent value is a reasonable fill.

  3. What does sns.barplot(data=df, x="Sex", y="Survived") show, given Survived is 0 or 1?

    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.

  4. What does df.pivot_table("Survived", index="Pclass", columns="Sex", aggfunc="mean") produce?

    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

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Survival rate by sex

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Missing value check

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did