PYTHON FOR AI • LESSON 3

Pandas Project

In this project, we will use Pandas to analyze a small sales dataset. You will combine the concepts learned in this lesson: DataFrames, CSV files, filtering, sorting, missing data, and GroupBy.

PROJECT GOAL

Turn raw sales data into useful information.

A real dataset is not useful just because it is inside a DataFrame. We need to inspect it, clean it, filter it, analyze it, and finally extract useful information. That is exactly what we will do in this project.

01

Create the Sales Dataset

We will work with customer sales data containing the customer name, city, product, sales amount, and age.

import pandas as pd

sales = pd.DataFrame({
    "Customer": [
        "Raj",
        "John",
        "Sarah",
        "David",
        "Emma",
        "Michael",
        "Sophia",
        "Daniel"
    ],
    "City": [
        "Hyderabad",
        "London",
        "Hyderabad",
        "New York",
        "London",
        "Hyderabad",
        "New York",
        "London"
    ],
    "Product": [
        "Laptop",
        "Phone",
        "Phone",
        "Laptop",
        "Tablet",
        "Laptop",
        "Phone",
        "Tablet"
    ],
    "Sales": [
        1200,
        800,
        600,
        1500,
        700,
        1800,
        900,
        1000
    ],
    "Age": [
        29,
        35,
        None,
        42,
        31,
        38,
        None,
        27
    ]
})

print(sales)

Notice that two customers have missing ages.

02

Inspect the Data

Before analyzing data, don't immediately start calculating things. First understand what is inside the DataFrame.

View the first rows

print(sales.head())

Check the DataFrame shape

print(sales.shape)
(8, 5)

This means:

8 rows
5 columns

Check column information

print(sales.info())
03

Find Missing Data

Before performing analysis, check whether values are missing.

print(sales.isna().sum())
Customer    0
City        0
Product     0
Sales       0
Age         2
dtype: int64

Only the Age column contains missing values.

04

Handle Missing Data

For this project, we will replace missing ages with the median age.

median_age = sales["Age"].median()

sales["Age"] = sales["Age"].fillna(
    median_age
)

print(sales)

Why median?

Age is numeric, and using the median gives us a reasonable replacement without simply inventing an age such as zero.

05

Filter the Sales Data

Now let's find customers who spent more than 1000.

high_sales = sales[
    sales["Sales"] > 1000
]

print(high_sales)

The condition:

sales["Sales"] > 1000

creates a Boolean condition. Pandas keeps only the rows where the condition is True.

06

Sort the Data

Let's find the customers with the highest sales.

sorted_sales = sales.sort_values(
    by="Sales",
    ascending=False
)

print(sorted_sales)

ascending=False means the largest value comes first.

Highest Sales
      ↓
      ↓
Lowest Sales
07

Calculate Total Sales by City

Now we use one of the most important Pandas concepts: GroupBy.

city_sales = sales.groupby(
    "City",
    as_index=False
)["Sales"].sum()

print(city_sales)
        City  Sales
0  Hyderabad   3600
1     London   1800
2   New York   2400

Instead of looking at individual customers, we now have a summary for each city.

08

Calculate Average Sales

Total sales tells us how much each city sold. But we can also calculate the average sale.

average_sales = sales.groupby(
    "City",
    as_index=False
)["Sales"].mean()

print(average_sales)
        City        Sales
0  Hyderabad  1200.000000
1     London   933.333333
2   New York  1200.000000

Now we can compare cities based on average transaction value rather than total sales.

09

Analyze Sales by Product

GroupBy does not have to use City. We can group by Product as well.

product_sales = sales.groupby(
    "Product",
    as_index=False
)["Sales"].sum()

print(product_sales)
  Product  Sales
0  Laptop   4500
1   Phone   2300
2  Tablet   1700

Now we can immediately see which products generated the most sales.

10

Find the Best-Selling Product

We can combine GroupBy and sorting.

product_sales = sales.groupby(
    "Product",
    as_index=False
)["Sales"].sum()

product_sales = product_sales.sort_values(
    by="Sales",
    ascending=False
)

print(product_sales)
  Product  Sales
0  Laptop   4500
1   Phone   2300
2  Tablet   1700

Because the data is sorted in descending order, the first row is the best-selling product.

11

Answer Business Questions

This is where Pandas becomes useful. Instead of learning commands just for the sake of learning them, use them to answer questions.

Question 1 — Which city generated the most sales?

city_sales = sales.groupby(
    "City",
    as_index=False
)["Sales"].sum()

city_sales = city_sales.sort_values(
    by="Sales",
    ascending=False
)

print(city_sales.head(1))

Question 2 — Which product generated the most sales?

product_sales = sales.groupby(
    "Product",
    as_index=False
)["Sales"].sum()

product_sales = product_sales.sort_values(
    by="Sales",
    ascending=False
)

print(product_sales.head(1))
12

Create a Complete Summary

We can create a summary containing several useful statistics.

summary = sales.groupby(
    "City",
    as_index=False
).agg(
    total_sales=("Sales", "sum"),
    average_sales=("Sales", "mean"),
    number_of_customers=("Customer", "count")
)

print(summary)
        City  total_sales  average_sales  number_of_customers
0  Hyderabad         3600         1200.0                    3
1     London         2800          933.3                    3
2   New York         2400         1200.0                    2

This is much more useful than simply printing the raw dataset.

We have transformed individual customer records into a useful business summary.

13

Complete Project Code

Here is the complete project from start to finish.

import pandas as pd


# 1. Create the dataset
sales = pd.DataFrame({
    "Customer": [
        "Raj",
        "John",
        "Sarah",
        "David",
        "Emma",
        "Michael",
        "Sophia",
        "Daniel"
    ],
    "City": [
        "Hyderabad",
        "London",
        "Hyderabad",
        "New York",
        "London",
        "Hyderabad",
        "New York",
        "London"
    ],
    "Product": [
        "Laptop",
        "Phone",
        "Phone",
        "Laptop",
        "Tablet",
        "Laptop",
        "Phone",
        "Tablet"
    ],
    "Sales": [
        1200,
        800,
        600,
        1500,
        700,
        1800,
        900,
        1000
    ],
    "Age": [
        29,
        35,
        None,
        42,
        31,
        38,
        None,
        27
    ]
})


# 2. Inspect the data
print(sales.head())
print(sales.shape)
print(sales.info())


# 3. Check missing values
print(sales.isna().sum())


# 4. Fill missing ages with median
median_age = sales["Age"].median()

sales["Age"] = sales["Age"].fillna(
    median_age
)


# 5. Filter high-value sales
high_sales = sales[
    sales["Sales"] > 1000
]

print(high_sales)


# 6. Sort sales
sorted_sales = sales.sort_values(
    by="Sales",
    ascending=False
)

print(sorted_sales)


# 7. Total sales by city
city_sales = sales.groupby(
    "City",
    as_index=False
)["Sales"].sum()

print(city_sales)


# 8. Average sales by city
average_sales = sales.groupby(
    "City",
    as_index=False
)["Sales"].mean()

print(average_sales)


# 9. Total sales by product
product_sales = sales.groupby(
    "Product",
    as_index=False
)["Sales"].sum()

print(product_sales)


# 10. Sort products by sales
product_sales = product_sales.sort_values(
    by="Sales",
    ascending=False
)

print(product_sales)


# 11. Create final city summary
summary = sales.groupby(
    "City",
    as_index=False
).agg(
    total_sales=("Sales", "sum"),
    average_sales=("Sales", "mean"),
    number_of_customers=("Customer", "count")
)

print(summary)
14

What You Learned

This project combined the main Pandas concepts from this lesson.

DataFrame
    ↓
Inspect Data
    ↓
Find Missing Values
    ↓
Clean Data
    ↓
Filter Data
    ↓
Sort Data
    ↓
GroupBy
    ↓
Calculate Statistics
    ↓
Create Summary
    ↓
Understand the Data

This workflow is much closer to what you actually do when working with real datasets than simply learning individual Pandas commands.

15

Why This Matters for AI

Pandas is not the AI model itself. It is one of the tools you use to prepare and understand the data before building Machine Learning or AI systems.

Raw Data
   ↓
Pandas
   ↓
Clean Data
   ↓
Analyze Data
   ↓
Prepare Features
   ↓
Machine Learning Model

If the data is wrong, incomplete, or misunderstood, a powerful AI model will not magically fix it.

KEY TAKEAWAY

Pandas is about turning raw data into useful information.

In this project, you created a DataFrame, inspected the data, found and handled missing values, filtered rows, sorted records, grouped data, calculated statistics, and created summaries. These are the core skills you need before moving deeper into Data Science, Machine Learning, and AI.