Lesson 4: Project 2 (More Practice)

Material

Download the CSV file. We will be using in this course. Use this Video to help you upload this file into Jupyter Notebook.

ACTION #1: DOWNLOAD YOUR DATASET

Practice First, Then Copy If Needed

In this project I recommend that you try typing the code yourself first.

Typing the code helps you:

  • Learn Python syntax naturally

  • Understand where commas, quotes, and parentheses belong

  • Build confidence reading real scripts

  • Catch mistakes and learn how to fix them (this is part of learning!)

Go slowly and run the notebook one section at a time using:

Shift + Enter

If You Get Stuck — You Can Copy the Code Below

If you run into repeated syntax errors or typing is difficult, you can copy and paste the code provided below.

This is especially helpful if:

  • You are using a screen reader

  • Small typos are preventing the code from running

  • You want to check your work

  • You want to focus on understanding the logic instead of typing

A good method is:

  • Try typing the code first,

  • Run it

If it doesn't work, compare it with the code below. Then copy it and continue the lesson.

Remember — the goal is to understand what the code is doing, not to struggle with typing.

** Tip: Even professional developers copy and reuse code. What matters most is understanding how and why it works.

Code for this Lesson:

# Step 1: Saving the Cleaned Dataset

#Saving the cleaned Dataset in Python (Pandas)

df.to_csv('cleaned_customer_data.csv', index=False)

# Step 2: Load Cleaned Dataset

import pandas as pd

df = pd.read_csv('cleaned_customer_data.csv')

# Step 3: Box Plot: Purchase Amount by Age Group

import seaborn as sns

import matplotlib.pyplot as plt

plt.figure(figsize=(10,6))

sns.boxplot(x='Age Group', y='Purchase Amount', data=df, palette='pastel')

plt.title('Spending Distribution by Age Group')

plt.xlabel('Age Group')

plt.ylabel('Purchase Amount')

plt.xticks(rotation=45)

plt.show()

# Step 4: Heatmap: Purchase Frequency by Age Group and Season

heat_data = df.pivot_table(index='Age Group', columns='Season', values='Purchase Amount', aggfunc='count')

plt.figure(figsize=(8,6))

sns.heatmap(heat_data, annot=True, fmt='d', cmap='YlGnBu')

plt.title('Purchasing Frequency by Age Group and Season')

plt.xlabel('Season')

plt.ylabel('Age Group')

plt.show()

# Step 5: Histogram: Distribution of Purchase Amounts

plt.figure(figsize=(10, 6))

sns.histplot(df['Purchase Amount'], bins=20, kde=True, color='skyblue')

plt.title('Distrbution of Purchase Amounts')

plt.xlabel('Purchase Amount')

plt.show()

# Step 6: Scatter Plot:Age v Purchase Amount by Gender

plt.figure(figsize=(10, 6))

sns.scatterplot(x='Age', y='Purchase Amount', hue='Gender', data=df, palette='Set2')

plt.title('Age vs Purchae Amount by Gender')

plt.xlabel('Age')

plt.ylabel('Purchase Amount')

plt.show()

# Step 7: Save the cleaned and analyzed dataset

from matplotlib.backends.backend_pdf import PdfPages

with PdfPages('data_visulization_report.pdf') as pdf:

# 1. Box Plot

plt.figure(figsize=(10,6))

sns.boxplot(x='Age Group', y='Purchase Amount', data=df, palette='pastel')

plt.title('Spending Distribution by Age Group')

plt.xticks(rotation=45)

pdf.savefig()

plt.close()

# 2. Heatmap

heat_data = df.pivot_table(index='Age Group', columns='Season', values='Purchase Amount', aggfunc='count')

plt.figure(figsize=(8,6))

sns.heatmap(heat_data, annot=True, fmt='d', cmap='YlGnBu')

plt.title('Purchasing Frequency by Age Group and Season')

pdf.savefig()

plt.close()

# 3. Histogram

plt.figure(figsize=(10, 6))

sns.histplot(df['Purchase Amount'], bins=20, kde=True, color='skyblue')

plt.title('Distrbution of Purchase Amounts')

pdf.savefig()

plt.close()

#4. Scattoer Plot

plt.figure(figsize=(10, 6))

sns.scatterplot(x='Age', y='Purchase Amount', hue='Gender', data=df, palette='Set2')

plt.title('Age vs Purchae Amount by Gender')

pdf.savefig()

plt.close()