0% found this document useful (0 votes)
4 views8 pages

Python Code CA-2

The document outlines a Python project focused on analyzing supermarket sales data using Pandas in a Jupyter notebook. It includes steps for importing the dataset, data cleaning, feature engineering, and various analyses such as descriptive statistics and visualizations. Key insights reveal sales performance variations across branches, popular product lines, and payment methods, along with the correlation between quantity and sales value.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views8 pages

Python Code CA-2

The document outlines a Python project focused on analyzing supermarket sales data using Pandas in a Jupyter notebook. It includes steps for importing the dataset, data cleaning, feature engineering, and various analyses such as descriptive statistics and visualizations. Key insights reveal sales performance variations across branches, popular product lines, and payment methods, along with the correlation between quantity and sales value.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

17/12/2025, 00:11 Python CA-2 working.

ipynb - Colab

keyboard_arrow_down PART - 1 DATASET IMPORTING

[Link] the Dataset

import pandas as pd

df = pd.read_csv('/content/drive/MyDrive/Python CA-2 /Supermarket Sales [Link]')


[Link]()
print(df)

Date Branch Customer type Gender Product line \


0 1/1/2024 Brooklyn Member Female Food & Beverages
1 1/1/2024 Queens Normal Female Electronics
2 1/1/2024 Brooklyn Normal Female Electronics
3 1/1/2024 Queens Member Female Sports & Travel
4 1/1/2024 Manhattan Member Female Sports & Travel
... ... ... ... ... ...
5048 12/31/2024 Manhattan Normal Female Food & Beverages
5049 12/31/2024 Brooklyn Normal Male Food & Beverages
5050 12/31/2024 Queens Member Male Electronics
5051 12/31/2024 Manhattan Member Female Sports & Travel
5052 12/31/2024 Queens Normal Female Health & Beauty

Unit price Quantity Payment Rating


0 84.63 10 Credit card 9.0
1 63.22 2 Cash 8.5
2 74.71 6 Cash 6.7
3 36.98 10 Credit card 7.0
4 27.04 4 Ewallet 6.9
... ... ... ... ...
5048 6.71 10 Cash 5.9
5049 6.79 9 Credit card 4.8
5050 85.05 7 Ewallet 4.4
5051 54.64 10 Credit card 4.8
5052 49.75 7 Cash 5.8

[5053 rows x 9 columns]

2. Understand the dataset structure

[Link]()

Date Branch Customer type Gender Product line Unit price Quantity Payment Rating

0 1/1/2024 Brooklyn Member Female Food & Beverages 84.63 10 Credit card 9.0

1 1/1/2024 Queens Normal Female Electronics 63.22 2 Cash 8.5

2 1/1/2024 Brooklyn Normal Female Electronics 74.71 6 Cash 6.7

3 1/1/2024 Queens Member Female Sports & Travel 36.98 10 Credit card 7.0

4 1/1/2024 Manhattan Member Female Sports & Travel 27.04 4 Ewallet 6.9

Next steps: Generate code with df New interactive sheet

[Link]

(5053, 9)

[Link]()

<class '[Link]'>
RangeIndex: 5053 entries, 0 to 5052
Data columns (total 9 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Date 5053 non-null object
1 Branch 5053 non-null object
2 Customer type 5053 non-null object
3 Gender 5053 non-null object
4 Product line 5053 non-null object
5 Unit price 5053 non-null float64
6 Quantity 5053 non-null int64
7 Payment 5053 non-null object
8 Rating 5053 non-null float64
dtypes: float64(2), int64(1), object(6)
memory usage: 355.4+ KB

[Link] 1/8
17/12/2025, 00:11 Python CA-2 [Link] - Colab

3. Identify Variable Types

numerical_cols = df.select_dtypes(include=['int64','float64']).columns
categorical_cols = df.select_dtypes(include=['object']).columns

print("Numerical Columns:", numerical_cols)


print("Categorical Columns:", categorical_cols)

Numerical Columns: Index(['Unit price', 'Quantity', 'Rating'], dtype='object')


Categorical Columns: Index(['Date', 'Branch', 'Customer type', 'Gender', 'Product line', 'Payment'], dtype='object')

keyboard_arrow_down PART 2 Data Wrangling, Cleaning & Preprocessing

1. Check Missing Values

[Link]().sum()

Date 0

Branch 0

Customer type 0

Gender 0

Product line 0

Unit price 0

Quantity 0

Payment 0

Rating 0

Total_Sales 0

dtype: int64

2. REMOVE DUPLICATE RECORD

df = df.drop_duplicates()
print("Shape after removing duplicates:", [Link])

Shape after removing duplicates: (5053, 10)

3. DATA TYPE CORRECTION

df['Date'] = pd.to_datetime(df['Date'])

4. OUTLIER DETECTION USING INTER QUARTILE RANGE

Q1 = df['Unit price'].quantile(0.25)
Q3 = df['Unit price'].quantile(0.75)
IQR = Q3 - Q1

lower_limit = Q1 - 1.5 * IQR


upper_limit = Q3 + 1.5 * IQR

df = df[(df['Unit price'] >= lower_limit) & (df['Unit price'] <= upper_limit)]


print("Shape after outlier removal:", [Link])

Shape after outlier removal: (5053, 10)

keyboard_arrow_down Feature Engineering


1. TOTAL SALES VALUE
2. EXTRACT MONTH

[Link] 2/8
17/12/2025, 00:11 Python CA-2 [Link] - Colab

df['Total_Sales'] = df['Unit price'] * df['Quantity']

df['Month'] = df['Date'].dt.month_name()

[Link]()

Customer Unit
Date Branch Gender Product line Quantity Payment Rating Total_Sales Month
type price

2024-01- Food & Credit


0 Brooklyn Member Female 84.63 10 9.0 846.30 January
01 Beverages card

2024-01-
1 Queens Normal Female Electronics 63.22 2 Cash 8.5 126.44 January
01

2024-01-
2 Brooklyn Normal Female Electronics 74.71 6 Cash 6.7 448.26 January
01

Next steps: Generate code with df New interactive sheet

6. NUMPY OPERATIONS

import numpy as np
[Link](df['Total_Sales'])
[Link](df['Quantity'])

10

7. FILTERING AND SORTING

HIGH VALUE TRANSACTION


SORT BY HIGHEST SALES

high_sales = df[df['Total_Sales'] > 500]


df.sort_values(by='Total_Sales', ascending=False).head()

Customer Product Unit


Date Branch Gender Quantity Payment Rating Total_Sales Month
type line price

2024- Fashion & Credit


527 Queens Member Female 99.30 10 6.6 993.0 February
02-15 Accs card

2024- Fashion & Credit


1106 Queens Member Female 99.30 10 6.6 993.0 April
04-05 Accs card

2024- Fashion & Credit


450 Manhattan Normal Male 98.98 10 8.7 989.8 February
02-08 Accs card

keyboard_arrow_down PART - 3 DATA ANALYSIS

1. Descriptive Statitics

[Link]()

Date Unit price Quantity Rating Total_Sales

count 5053 5053.000000 5053.000000 5053.000000 5053.000000

mean 2024-07-15 14:07:14.593311232 39.692988 4.593509 6.884029 186.531734

min 2024-01-01 00:00:00 2.010000 1.000000 3.000000 2.180000

25% 2024-04-14 00:00:00 11.630000 2.000000 5.600000 38.520000

50% 2024-07-19 00:00:00 33.900000 4.000000 7.100000 109.600000

75% 2024-10-21 00:00:00 64.940000 7.000000 8.200000 266.560000

max 2024-12-31 00:00:00 99.960000 10.000000 10.000000 993.000000

std NaN 29.557474 2.787934 1.710245 200.196697

2. Univariate Analysis

[Link] 3/8
17/12/2025, 00:11 Python CA-2 [Link] - Colab
df['Branch'].value_counts()

count

Branch

Brooklyn 1793

Manhattan 1705

Queens 1555

dtype: int64

df['Product line'].value_counts()

count

Product line

Food & Beverages 1670

Health & Beauty 958

Home & Lifestyle 708

Fashion & Accs 676

Sports & Travel 576

Electronics 465

dtype: int64

3. BIVARIATE ANALYSIS

[Link]('Branch')['Total_Sales'].mean()

Total_Sales

Branch

Brooklyn 178.307574

Manhattan 181.816158

Queens 201.185093

dtype: float64

[Link]('Payment')['Total_Sales'].sum()

Total_Sales

Payment

Cash 304622.65

Credit card 311230.89

Ewallet 326691.31

dtype: float64

keyboard_arrow_down PART - 4 DATA VISUALISATION

1. LINE CHART

monthly_sales = [Link]('Month')['Total_Sales'].sum()

[Link]()
monthly_sales.plot(marker='o')
[Link]('Monthly Sales Trend')
[Link]('Month')
[Link]('Total Sales')
[Link]()

[Link] 4/8
17/12/2025, 00:11 Python CA-2 [Link] - Colab

Interpretation Of Line Chart - Sales show noticeable month-to-month variation, indicating seasonal demand patterns in the
supermarket.

2. HISTOGRAM CHART

[Link]()
[Link](df['Unit price'], bins=20)
[Link]('Distribution of Unit Price')
[Link]('Unit Price')
[Link]('Frequency')
[Link]()

Interpretation - Most products are priced in the mid-range, with very few items at extremely low or high prices.

3. BAR CHART

[Link]()
[Link]('Branch')['Total_Sales'].sum().plot(kind='bar')
[Link]('Total Sales by Branch')
[Link]('Branch')
[Link]('Total Sales')
[Link]()

[Link] 5/8
17/12/2025, 00:11 Python CA-2 [Link] - Colab

Interpretation - Sales performance differs across branches, with some branches contributing significantly more to total revenue.

4. SEABORN COUNTPLOT

import seaborn as sns


[Link]()
[Link](x='Payment', data=df)
[Link]('Payment Method Preference')
[Link]()

Interpretation - Digital payments, especially credit cards, are the most preferred payment method among customers.

5. SEABORN BOXPLOT

[Link](figsize=(10,5))
[Link](x='Product line', y='Total_Sales', data=df)
[Link](rotation=45)
[Link]('Sales Distribution by Product Line')
[Link]()

[Link] 6/8
17/12/2025, 00:11 Python CA-2 [Link] - Colab

Interpretation - Certain product lines generate higher and more consistent sales, while others show greater variability.

6. HEATMAP

[Link](figsize=(6,4))
[Link](
df[['Unit price', 'Quantity', 'Rating', 'Total_Sales']].corr(),
annot=True,
cmap='coolwarm'
)
[Link]('Correlation Heatmap')
[Link]()

Interprettaion - Highlighted correlation among numerical variables.

KEY INSIGHTS

1. Sales performance varies across branches.


2. Food & Beverages is the most popular product line.
3. Credit card is the most used payment method.
4. Quantity has a stronger impact on sales than unit price.
5. Ratings are moderately correlated with sales value.

[Link] 7/8
17/12/2025, 00:11 Python CA-2 [Link] - Colab

[Link] 8/8

You might also like