-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustomer_points_v2.py
More file actions
52 lines (39 loc) · 1.58 KB
/
Copy pathcustomer_points_v2.py
File metadata and controls
52 lines (39 loc) · 1.58 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
# -*- coding: utf-8 -*-
"""
Customer Loyalty Points Engine v2 — Discover Diving
Refactored 2024
Functionally identical to v1 but rewritten using vectorised Pandas
operations. Replaces manual iteration with groupby aggregation,
improving readability, performance, and scalability.
"""
import pandas as pd
POINTS_RATE = 0.025 # 2.5% of net sales
def load_data(receipts_path: str, customers_path: str) -> tuple[pd.DataFrame, pd.DataFrame]:
receipts = pd.read_csv(receipts_path)
customers = pd.read_csv(customers_path)
return receipts, customers
def calculate_points(receipts: pd.DataFrame) -> pd.DataFrame:
return receipts.groupby('Customer name').agg(
total_spent=('Net sales', 'sum'),
points_balance=('Net sales', lambda x: (x * POINTS_RATE).sum()),
total_visits=('Net sales', 'count')
).reset_index()
def reconcile_balances(customers: pd.DataFrame, points: pd.DataFrame) -> pd.DataFrame:
updated = customers.merge(
points[['Customer name', 'points_balance']],
on='Customer name',
how='left'
)
updated['Points balance'] = updated['points_balance'].fillna(0)
return updated.drop(columns=['points_balance'])
def main():
receipts, customers = load_data(
"receipts-2021-12-31-2022-03-15.csv",
"customers-2022-03-15.csv"
)
points = calculate_points(receipts)
updated_customers = reconcile_balances(customers, points)
updated_customers.to_csv("customerUpdatedPoints.csv", index=False)
print(f"Updated points for {len(updated_customers)} customers.")
if __name__ == "__main__":
main()