Sales Data Analysis Project Using NumPy
In this practical project, we will analyze monthly sales data for different products using NumPy. We will calculate total sales, product-wise performance, monthly revenue, average sales, highest and lowest values, month-to-month growth, and sales rankings. We will also filter sales records and export the results to CSV and NPZ files.
1. Project Objectives
After completing this project, you will be able to:
- Create NumPy arrays representing products and monthly sales.
- Calculate product-wise and month-wise sales totals.
- Find average, maximum, and minimum sales.
- Identify the best-performing product and month.
- Calculate sales growth between consecutive months.
- Filter products using Boolean conditions.
- Sort products according to total sales.
- Export sales reports to CSV and save arrays in NPZ format.
2. Understand the Project Dataset
Suppose CIIT TRANING INSTITUTE is teaching students how a business can analyze sales data. We will use three sample products and their sales over four months. All values represent sales revenue in rupees.
| Product | January | February | March | April |
|---|---|---|---|---|
| Product A | 12000 | 15000 | 14000 | 18000 |
| Product B | 10000 | 11000 | 13000 | 12500 |
| Product C | 8000 | 9500 | 10500 | 12000 |
3. Import NumPy and Create Sales Arrays
First, import NumPy and create arrays for the product names, month names, and numerical sales data.
Expected Output
The array contains 3 rows and 4 columns. Therefore, it stores 12 sales values. NumPy lets us perform calculations on the entire dataset without writing a separate calculation for every product and month.
4. Inspect the Sales Array
Use the shape, ndim, and size attributes to
understand the structure of the dataset.
Expected Output
The displayed integer type may vary by platform or NumPy environment. The important point is that the array stores integer sales values.
5. Calculate Total Sales for Each Product
Use np.sum() with axis=1 to add the monthly values across
each product's row.
Expected Output
| Product | Total Sales |
|---|---|
| Product A | ₹59,000 |
| Product B | ₹46,500 |
| Product C | ₹40,000 |
axis=1 calculates across each row. Here, it
adds January, February, March, and April sales for each product.
6. Calculate Total Sales for Each Month
To calculate monthly totals across all products, use axis=0.
NumPy adds the values down each column.
Expected Output
April has the highest total revenue of ₹42,500, while January has the lowest total revenue of ₹30,000 in this dataset.
7. Calculate Average Sales
The np.mean() function calculates averages. We can calculate the
average monthly sales for each product and the average revenue across all products
for each month.
Expected Output
Product A has average monthly sales of ₹14,750. The average sales per product in April are approximately ₹14,166.67.
8. Find the Highest and Lowest Sales
Use np.max() and np.min() to find the highest and lowest
monthly sales for every product.
Expected Output
Product A's highest monthly revenue is ₹18,000. The lowest value in the entire dataset is ₹8,000 for Product C.
9. Identify the Best-Performing Product
The np.argmax() function returns the index of the largest value.
Use this index to find the product with the highest total sales.
Expected Output
Product A is the best-performing product by total revenue during the four-month period. This conclusion is based on revenue alone and does not account for costs or profit margins.
10. Find the Best and Worst Sales Months
Apply np.argmax() and np.argmin() to the monthly totals.
These functions return the positions of the largest and smallest values.
Expected Output
11. Calculate Month-to-Month Sales Growth
Use np.diff() to calculate the difference between consecutive monthly
totals. Positive differences represent an increase, while negative differences
represent a decrease.
Expected Output
Revenue increased by ₹5,500 from January to February, by ₹2,000 from February to March, and by ₹5,000 from March to April.
Calculate Percentage Growth
To calculate percentage growth, divide each difference by the previous month's total and multiply by 100.
Expected Output
These percentages represent January-to-February, February-to-March, and March-to-April growth, respectively.
12. Filter Products Using Conditions
Boolean indexing lets us select values based on a condition. In this example, we select products whose total sales are greater than ₹45,000.
Expected Output
Filter Individual Monthly Values
You can also select sales values above ₹12,000 from the complete two-dimensional array.
Expected Output
When a Boolean condition is applied directly to a two-dimensional array, the selected values are returned as a one-dimensional array.
13. Sort Products by Total Sales
Use np.argsort() to obtain the indices that would sort product totals.
Reverse the indices to display products from highest to lowest sales.
Expected Output
np.argsort() returns indices, not product
names. Use the returned indices to retrieve the matching product names and totals.
14. Calculate Each Product's Contribution
We can calculate the percentage contribution of each product to total revenue. First, find the sum of all product totals, then divide each product total by that sum.
Expected Output
Product A contributes approximately 40.55% of the total revenue, followed by Product B at 31.96% and Product C at 27.49%.
15. Export the Sales Report to CSV
NumPy performs the calculations, while Python's built-in csv module
writes product names, monthly sales, and calculated totals into a spreadsheet-friendly
file.
Expected Output
The file sales_analysis_report.csv is created in the current working
directory. You can open it in a spreadsheet application to inspect or share the report.
16. Save and Load Sales Data Using NPZ
The NPZ format stores multiple NumPy arrays in a single archive. This is useful when you want to preserve both the original sales data and the calculated results.
Expected Output
np.save() for one array,
np.savez() for multiple arrays, and
np.savez_compressed() to compress the saved archive.
17. Complete Project Code
The following script combines the main analysis operations, rankings, growth calculations, CSV export, and NPZ saving into one runnable program.
Run the script in your Python environment. It prints the sales report and creates
sales_analysis_report.csv and sales_analysis_data.npz in
the current working directory.
18. Common Mistakes
Mistake 1: Using the wrong axis
axis=1 calculates across each product's row. axis=0
calculates down the columns to produce month-wise results.
Mistake 2: Confusing revenue with profit
Sales revenue is the amount earned from sales. Profit requires cost information as well. This dataset does not include costs, so it cannot determine profit.
Mistake 3: Sorting totals without preserving product names
np.argsort() returns indices. Use the same indices to access both
the product names and their totals.
Mistake 4: Misinterpreting np.diff()
np.diff() returns absolute differences, not percentages. Divide each
difference by the previous value and multiply by 100 to calculate growth rates.
Mistake 5: Expecting CSV or NPZ files in a specific folder
Relative filenames are saved in the current working directory. Use an explicit path when you need the files to be stored in a particular location.
19. Practice Exercises
- Add May sales for all three products and update the analysis.
- Find the product with the lowest total revenue.
- Calculate each product's percentage contribution to grand total sales.
- Display only products whose total sales exceed ₹45,000.
- Find the month with the largest increase in revenue.
- Calculate the difference between the highest and lowest monthly totals.
- Change Product C's March sales to ₹15,000 and recalculate the report.
- Load the NPZ archive and print the saved monthly totals.
- Extend the dataset to six months and calculate the growth percentages.
Summary
In this project, you used NumPy arrays to analyze product-wise and month-wise sales, calculate totals and averages, identify highest and lowest values, measure month-to-month changes, filter records, rank products, and calculate revenue contributions.
You also exported a report to CSV and saved the numerical dataset in compressed NPZ format. These techniques can be adapted to business reports, inventory analysis, monthly revenue tracking, and other numerical data-analysis tasks.