-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsales2.py
More file actions
60 lines (51 loc) · 2.54 KB
/
sales2.py
File metadata and controls
60 lines (51 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Read data
sale = pd.read_csv("sales2.csv")
print(sale)
# Make sure that Price and Quantity are numbers.
sale["Price"] = pd.to_numeric(sale["Price"], errors="coerce")
sale["Quantity"] = pd.to_numeric(sale["Quantity"], errors="coerce")
# calculate the total
sale["total"] = sale["Price"] * sale["Quantity"]
print(sale["total"])
#Converts the "Product" and "total" columns from the DataFrame sale into a NumPy array and stores it in data.
data = sale[["Product", "total"]].to_numpy()
just_5 = data[0:5,:] #Selects the first 5 rows from the data array (all columns) and stores them in just_5.
just_product = data[:,0] #Extracts the first column (i.e., the "Product" names) from data and stores it in just_product.
just_revenue = data[:,1] #Extracts the second column (i.e., the "total" revenue values) from data and stores it in just_revenue.
revenue_just_last_3 = data[7:,1] #Selects the revenue values (second column) starting from the 8th row to the end and stores them in revenue_just_last_3.
print (f"first five lines :\n{just_5}\njust products :\n{just_product}\njust revenue : {just_revenue}\nlast three products revenue : {revenue_just_last_3}\n")
# calculate Total revenues
total = np.sum(just_revenue)
print(f"Total revenues : {total}")
# Create a product list without duplication
unique = np.unique(data[:,0])
print(unique)
# make a storages lists
products = []
revenues = []
#This loop calculates the total revenue for each unique product, prints the revenue and its percentage of the total, and stores the results.
for product in unique:
mask = data[:,0] == product
rows = data[mask][:,1].astype(float)
revenue = rows.sum()
print(f"{product} : {revenue:.2f} , {(revenue/total)*100:.2f}%")
products.append(product)
revenues.append(revenue)
# Generate different random colors for each column
colors = plt.cm.tab20(np.linspace(0, 1, len(products)))
# Output products as numerical values
x_labels = list(range(1, len(products) + 1))
plt.figure(figsize=(10,6))
plt.bar(range(len(products)), revenues, color=colors)
plt.title("Total Revenue per Product")
plt.xlabel("Product")
plt.ylabel("Revenue")
plt.xticks(ticks=range(len(products)), labels=range(1,len(products) + 1))
legend_text = "\n".join([f"{i+1} = {product}"for i,product in enumerate(products)])
plt.figtext(0.83,0.8, legend_text, fontsize=10, va="center")
plt.subplots_adjust(right=0.8)
plt.savefig("revenue_chart.pdf", bbox_inches="tight", dpi =300)
plt.show()