Data Preparation

Environment Setup & Loading Data

First, we need to load our toolkit. We will use Pandas for data manipulation and Seaborn/Matplotlib for visualisation.

import pandas as pd  # The "Excel" of Python
import seaborn as sns  # For statistical plotting
import matplotlib.pyplot as plt  # The engine behind the plots

# Force all pandas tables to display 2 decimal places
pd.options.display.float_format = "{:,.2f}".format

# Set a nice theme for our charts
sns.set_theme(style="whitegrid")

# Load the dataset directly from the seaborn package
df = sns.load_dataset("penguins")

# Show the first 5 rows to verify it loaded correctly
display(df.head())
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex
0 Adelie Torgersen 39.10 18.70 181.00 3,750.00 Male
1 Adelie Torgersen 39.50 17.40 186.00 3,800.00 Female
2 Adelie Torgersen 40.30 18.00 195.00 3,250.00 Female
3 Adelie Torgersen NaN NaN NaN NaN NaN
4 Adelie Torgersen 36.70 19.30 193.00 3,450.00 Female

Data Cleaning

Real-world data is rarely perfect. Before we can analyse the penguins, we need to check for missing values (NaNs).

# 1. Create a clean summary of missing values
missing_summary = df.isnull().sum().to_frame(name="Missing Values")
display(missing_summary)
Table 1: Summary of Missing Values
Missing Values
species 0
island 0
bill_length_mm 2
bill_depth_mm 2
flipper_length_mm 2
body_mass_g 2
sex 11

Since we have enough data, we will drop the rows with missing values to ensure our later analysis is accurate.

# Create a clean dataframe by dropping rows with any missing values
df_clean = df.dropna()

# Calculate how many rows were dropped
rows_removed = len(df) - len(df_clean)

print("-" * 30)
print(f"CLEANING REPORT")
print(f"• Original records: {len(df)}")
print(f"• Records kept:     {len(df_clean)}")
print(f"• Records removed:  {rows_removed}")
print("-" * 30)
------------------------------
CLEANING REPORT
• Original records: 344
• Records kept:     333
• Records removed:  11
------------------------------