74% of Women Lived. 19% of Men Didn't. The Data Explains Why
ā Runtime tested ā the solution passes every example case. š Full written solution: https://interview-kit-fe.vercel.app/project-build-a-exploring-the-titanic-dataset-beginner-pan š» Code on GitHub: https://github.com/timepasshub2539-hue/leetcode-python/tree/main/project-build-a-exploring-the-titanic-dataset-beginner-pan Chapters: 0:00 74% of Women Lived. 19% of Men Didn't. 0:17 The Plan 0:33 Step 1 ā Load and Look 0:51 Step 2 ā Find the Holes 1:08 Step 3 ā Survival by Sex 1:24 Step 4 ā Survival by Class 1:39 Step 5 ā Bucketing Age 1:59 Why Rates, Not Counts 2:16 Step 6 ā Plotting It 2:34 Step 7 ā Overlapping Histogram 2:56 The Complete Program 3:10 What Running It Looks Like 3:24 Take It Further 3:42 The Data Ranked Its Own Killers Learn Pandas by investigating the most famous dataset in data science. We load the Titanic CSV, hunt down missing values, then break survival down by sex, class, and age bucket. You'll see why rates beat raw counts, plot survival with bar charts, and build an overlapping histogram that reveals who the data blamed. Ends with a complete runnable program you can extend yourself. #Pandas #DataScience #Python #DataAnalysis #LearnToCode Watch next: - Maximum Product of Two Digits ā š¢ Beginner (1/3) ā LeetCode Daily Python Solution (the trick nobody sees): https://youtu.be/LcqYDC9_CSI - You've Used These 10 Functions Thousands of Times #shorts: https://youtu.be/oZu7t8eNNpM - Lesson: The 10 Python Functions You've Used 1,000 Times: https://youtu.be/Bemj6NGiRic
The solution
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.read_csv("train.csv")
print(df.shape)
print(df.isnull().sum())
print(df.groupby("Sex")["Survived"].mean())
print(df.groupby("Pclass")["Survived"].mean())
df["AgeGroup"] = pd.cut(df["Age"], [0,12,18,60,120],
labels=["child","teen","adult","senior"])
print(df.groupby("AgeGroup", observed=True)["Survived"].mean())
for col in ["Sex", "Pclass"]:
df.groupby(col)["Survived"].mean().plot(kind="bar")
plt.title(f"Survival rate by {col}"); plt.tight_layout()
plt.savefig(f"by_{col}.png"); plt.clf()
sns.histplot(data=df, x="Age", hue="Survived", stat="density",
common_norm=False, bins=20, element="step")
plt.savefig("age_hist.png")Ready to try it yourself? Solve General problems with instant feedback in the practice sandbox.
Practice now ā


