-
Notifications
You must be signed in to change notification settings - Fork 0
/
sales_discount_analysis.py
49 lines (42 loc) · 1.61 KB
/
sales_discount_analysis.py
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
import pandas as pd
import altair as alt
def sales_with_discount_analysis(df, filename="sales_discount_analysis.html"):
"""Анализ продаж с учетом и без учета скидок по категориям товаров."""
sales_by_category = (
df.groupby(["Category", "Discount"])["Sales"].sum().reset_index()
)
sales_by_category["SalesWithoutDiscount"] = sales_by_category.apply(
lambda r: r["Sales"] if r["Discount"] == 0 else 0, axis=1
)
sales_by_category["SalesWithDiscount"] = sales_by_category.apply(
lambda r: r["Sales"] if r["Discount"] > 0 else 0, axis=1
)
sales_summary = (
sales_by_category.groupby("Category")[
["SalesWithDiscount", "SalesWithoutDiscount"]
]
.sum()
.reset_index()
)
sales_melted = pd.melt(
sales_summary,
id_vars="Category",
value_vars=["SalesWithDiscount", "SalesWithoutDiscount"],
var_name="Type",
value_name="Sales",
)
chart = (
alt.Chart(sales_melted)
.mark_bar()
.encode(
x=alt.X("Category:N", title="Категория"),
y=alt.Y("Sales:Q", title="Продажи"),
color=alt.Color(
"Type:N", title="Тип продаж", scale=alt.Scale(scheme="spectral")
),
)
.properties(width=600, height=400, title="Продажи по категориям товаров")
.interactive()
)
chart.save(filename)
print(f"График анализа продаж по скидкам сохранен в '{filename}'")