Get Revenue for a given product_id¶
Use order items data set and compute total revenue generated for a given product_id.
- Filter for given product_id.
- Extract order_item_subtotal for each item.
- Aggregate to get the revenue for 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]:
order_item = order_items[0]
In [5]:
order_item
Out[5]:
'1,1,957,1,299.98,299.98'
In [6]:
order_item.split(',')
Out[6]:
['1', '1', '957', '1', '299.98', '299.98']
In [7]:
order_item.split(',')[2]
Out[7]:
'957'
In [8]:
int(order_item.split(',')[2])
Out[8]:
957
In [9]:
float(order_item.split(',')[4])
Out[9]:
299.98
In [10]:
items_for_product = filter(
lambda order_item: int(order_item.split(',')[2]) == 502,
order_items
)
In [11]:
list(items_for_product)[:10]
Out[11]:
['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 [12]:
items_for_product = filter(
lambda order_item: int(order_item.split(',')[2]) == 502,
order_items
)
item_subtotals = map(
lambda order_item: float(order_item.split(',')[4]),
items_for_product
)
In [13]:
list(item_subtotals)[:10]
Out[13]:
[250.0, 150.0, 50.0, 250.0, 50.0, 50.0, 250.0, 100.0, 50.0, 250.0]
In [14]:
len(list(item_subtotals))
Out[14]:
0
In [15]:
items_for_product = filter(
lambda order_item: int(order_item.split(',')[2]) == 502,
order_items
)
item_subtotals = map(
lambda order_item: float(order_item.split(',')[4]),
items_for_product
)
from functools import reduce
reduce(
lambda total_revenue, item_revenue: total_revenue + item_revenue,
item_subtotals
)
Out[15]:
3147800.0
{note}
We can also aggregate using functions such as `add`, `min`, `max` etc to get the aggregated results.
In [16]:
from operator import add
items_for_product = filter(
lambda order_item: int(order_item.split(',')[2]) == 502,
order_items
)
item_subtotals = map(
lambda order_item: float(order_item.split(',')[4]),
items_for_product
)
reduce(
add,
item_subtotals
)
Out[16]:
3147800.0
In [17]:
items_for_product = filter(
lambda order_item: int(order_item.split(',')[2]) == 502,
order_items
)
item_subtotals = map(
lambda order_item: float(order_item.split(',')[4]),
items_for_product
)
reduce(
min,
item_subtotals
)
Out[17]:
50.0