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 Pythonimport seaborn as sns # For statistical plottingimport matplotlib.pyplot as plt # The engine behind the plots# Force all pandas tables to display 2 decimal placespd.options.display.float_format ="{:,.2f}".format# Set a nice theme for our chartssns.set_theme(style="whitegrid")# Load the dataset directly from the seaborn packagedf = sns.load_dataset("penguins")# Show the first 5 rows to verify it loaded correctlydisplay(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 valuesmissing_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 valuesdf_clean = df.dropna()# Calculate how many rows were droppedrows_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
------------------------------