-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
57 lines (38 loc) · 1.78 KB
/
main.py
File metadata and controls
57 lines (38 loc) · 1.78 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
from classes.reader import Read
from classes.writer import Write
from classes.mapper import Map
import pandas as pd
#READ ORDERS
#----------------------
orders = Read('amazon-orders.csv')
orders_df = orders.df #['Order Date','Category','Quantity','Item Total']
orders_df = orders_df[['Order Date','Category','Quantity','Item Total']].copy(deep=False)
#print(df.head())
# DFINE CATEGORY MAPPINGS
#----------------------
mapper = Map('categories.csv','Category')
# REMOVE $ CHARACTER
#----------------------
orders_df['Total'] = (orders_df['Item Total'].replace( '[\$,)]','', regex=True ).astype(float))
# ORDERS BY MONTHLY SPEND
#----------------------
mapped_orders = mapper.map_data(orders_df,'Category')
# make a month column to preserve the order
mapped_orders['month'] = pd.to_datetime(mapped_orders['Order Date']).dt.strftime('%m')
# create the pivot table with this numeric month column
orders_df_pivot = mapped_orders.pivot_table(index='month',columns=['Parent Category'],values=['Total'],aggfunc=sum, fill_value=0).T
# print(orders_df_pivot)
Write(orders_df_pivot,'orders_by_monthly_spend')
# ORDERS GROUPED BY CATEGORY
#-------------------
orders_grouped = orders_df.groupby(['Category'],sort=True).sum().reset_index()
orders_grouped = orders_grouped.sort_values(by=['Total','Quantity'], ascending=[False,False])
#print(orders_grouped)
Write(orders_grouped,'orders_by_category')
# ORDERS GROUPED BY PARENT CATEGORY
#----------------------
mapped_grouped_orders = mapper.map_data(orders_grouped,'Category')
mapped_grouped_orders = mapped_grouped_orders.groupby(['Parent Category'],sort=True).sum().reset_index()
mapped_grouped_orders = mapped_grouped_orders.sort_values(by=['Total','Quantity'], ascending=[False,False])
#print(mapped_orders)
Write(mapped_grouped_orders,'orders_by_parent_category')