Get total items sold and revenue¶
Use order items data set and get total number of items sold as well as total revenue generated for a given product_id.
- Filter for items using
filter
related to a given product_id. - Apply
map
to get order_item_quantity and order_item_subtotal. - Use
reduce
to get total quantity and revenue for the items related to a given product_id.
In [1]:
%run 02_preparing_data_sets.ipynb
In [2]:
order_items[:10]
Out[2]:
['1,1,957,1,299.98,299.98', '2,2,1073,1,199.99,199.99', '3,2,502,5,250.0,50.0', '4,2,403,1,129.99,129.99', '5,4,897,2,49.98,24.99', '6,4,365,5,299.95,59.99', '7,4,502,3,150.0,50.0', '8,4,1014,4,199.92,49.98', '9,5,957,1,299.98,299.98', '10,5,365,5,299.95,59.99']
In [3]:
len(order_items)
Out[3]:
172198
In [4]:
t1 = (1, 200.0)
In [5]:
t2 = (2, 300.0)
In [6]:
res = (0, 0.0)
In [7]:
res = (res[0] + t1[0], res[1] + t1[1])
In [8]:
res
Out[8]:
(1, 200.0)
In [9]:
res = (res[0] + t2[0], res[1] + t2[1])
In [10]:
res
Out[10]:
(3, 500.0)
In [11]:
items_for_product = filter(
lambda order_item: int(order_item.split(',')[2]) == 502,
order_items
)
In [12]:
list(items_for_product)[:10]
Out[12]:
['3,2,502,5,250.0,50.0', '7,4,502,3,150.0,50.0', '20,8,502,1,50.0,50.0', '38,12,502,5,250.0,50.0', '42,14,502,1,50.0,50.0', '43,15,502,1,50.0,50.0', '60,20,502,5,250.0,50.0', '67,21,502,2,100.0,50.0', '70,24,502,1,50.0,50.0', '71,24,502,5,250.0,50.0']
In [13]:
items_for_product = filter(
lambda order_item: int(order_item.split(',')[2]) == 502,
order_items
)
item_details = map(
lambda order_item: (int(order_item.split(',')[3]), float(order_item.split(',')[4])),
items_for_product
)
In [14]:
list(item_details)[:10]
Out[14]:
[(5, 250.0), (3, 150.0), (1, 50.0), (5, 250.0), (1, 50.0), (1, 50.0), (5, 250.0), (2, 100.0), (1, 50.0), (5, 250.0)]
In [15]:
items_for_product = filter(
lambda order_item: int(order_item.split(',')[2]) == 502,
order_items
)
item_details = map(
lambda order_item: (int(order_item.split(',')[3]), float(order_item.split(',')[4])),
items_for_product
)
from functools import reduce
reduce(
lambda tot, ele: (tot[0] + ele[0], tot[1] + ele[1]),
item_details
)
Out[15]:
(62956, 3147800.0)