In [6]:
import pandas as pd
import numpy as np
# --- 1. Load the Dataset ---
# The '~' symbol is a shortcut for your home directory on macOS.
file_path = '~/Downloads/titanic.csv'
data = pd.read_csv(file_path)
print("--- Initial Data Info ---")
print(data.head())
print("\n")
# --- 2. Identify Missing Values ---
print("--- Missing Values Before Cleaning ---")
print(data.isnull().sum())
print("\n")
# --- 3. Clean the Data ---
# Fill missing values in the "Age" column with the mean age.
mean_age = data['Age'].mean()
data['Age'] = data['Age'].fillna(mean_age)
print(f"Filled {data['Age'].isnull().sum()} missing 'Age' values with the mean: {mean_age:.2f}\n")
# Drop the "Cabin" column due to too many missing values.
data.drop('Cabin', axis=1, inplace=True)
print("Dropped the 'Cabin' column.\n")
# Convert the "Sex" column to numeric (Male = 0, Female = 1).
data['Sex'] = data['Sex'].map({'male': 0, 'female': 1})
print("Converted 'Sex' column to numeric values.\n")
# --- 4. Display the Cleaned Dataset ---
print("--- Cleaned Data Info ---")
print(data.head())
print("\n")
print("--- Missing Values After Cleaning ---")
# We see 'Embarked' still has 2 missing values, which could be handled next.
print(data.isnull().sum())
print("\n")
print("--- Final Data Types ---")
print(data.info())
--- Initial Data Info ---
PassengerId Survived Pclass \
0 1 0 3
1 2 1 1
2 3 1 3
3 4 1 1
4 5 0 3
Name Sex Age SibSp \
0 Braund\t Mr. Owen Harris male 22.0 1
1 Cumings\t Mrs. John Bradley (Florence Briggs T... female 38.0 1
2 Heikkinen\t Miss Laina female 26.0 0
3 Futrelle\t Mrs. Jacques Heath (Lily May Peel) female 35.0 1
4 Allen\t Mr. William Henry male 35.0 0
Parch Ticket Fare Cabin Embarked
0 0 A/5 21171 7.2500 NaN S
1 0 PC 17599 71.2833 C85 C
2 0 STON/O2. 3101282 7.9250 NaN S
3 0 113803 53.1000 C123 S
4 0 373450 8.0500 NaN S
--- Missing Values Before Cleaning ---
PassengerId 0
Survived 0
Pclass 0
Name 0
Sex 0
Age 177
SibSp 0
Parch 0
Ticket 0
Fare 0
Cabin 687
Embarked 2
dtype: int64
Filled 0 missing 'Age' values with the mean: 29.70
Dropped the 'Cabin' column.
Converted 'Sex' column to numeric values.
--- Cleaned Data Info ---
PassengerId Survived Pclass \
0 1 0 3
1 2 1 1
2 3 1 3
3 4 1 1
4 5 0 3
Name Sex Age SibSp Parch \
0 Braund\t Mr. Owen Harris 0 22.0 1 0
1 Cumings\t Mrs. John Bradley (Florence Briggs T... 1 38.0 1 0
2 Heikkinen\t Miss Laina 1 26.0 0 0
3 Futrelle\t Mrs. Jacques Heath (Lily May Peel) 1 35.0 1 0
4 Allen\t Mr. William Henry 0 35.0 0 0
Ticket Fare Embarked
0 A/5 21171 7.2500 S
1 PC 17599 71.2833 C
2 STON/O2. 3101282 7.9250 S
3 113803 53.1000 S
4 373450 8.0500 S
--- Missing Values After Cleaning ---
PassengerId 0
Survived 0
Pclass 0
Name 0
Sex 0
Age 0
SibSp 0
Parch 0
Ticket 0
Fare 0
Embarked 2
dtype: int64
--- Final Data Types ---
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 11 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 PassengerId 891 non-null int64
1 Survived 891 non-null int64
2 Pclass 891 non-null int64
3 Name 891 non-null object
4 Sex 891 non-null int64
5 Age 891 non-null float64
6 SibSp 891 non-null int64
7 Parch 891 non-null int64
8 Ticket 891 non-null object
9 Fare 891 non-null float64
10 Embarked 889 non-null object
dtypes: float64(2), int64(6), object(3)
memory usage: 76.7+ KB
None
In [ ]: