Revenue per order using itertools¶
Get revenue per order using order_items data set.
In [1]:
%run 02_preparing_data_sets.ipynb
In [2]:
order_items[:4]
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']
In [3]:
order_subtotals = map(lambda oi: (int(oi.split(',')[1]), float(oi.split(',')[4])), order_items)
In [4]:
list(order_subtotals)[:3]
Out[4]:
[(1, 299.98), (2, 199.99), (2, 250.0)]
In [5]:
order_subtotals = map(lambda oi: (int(oi.split(',')[1]), float(oi.split(',')[4])), order_items)
order_subtotals_sorted = sorted(order_subtotals)
In [6]:
order_subtotals_sorted[:3]
Out[6]:
[(1, 299.98), (2, 129.99), (2, 199.99)]
In [7]:
import itertools as iter
In [8]:
order_subtotals_grouped = iter.groupby(order_subtotals_sorted, lambda rec: rec[0])
In [9]:
list(order_subtotals_grouped)[:3]
Out[9]:
[(1, <itertools._grouper at 0x7f8b5452c520>), (2, <itertools._grouper at 0x7f8b5452c4c0>), (4, <itertools._grouper at 0x7f8b5452cbb0>)]
In [10]:
order_subtotals = map(lambda oi: (int(oi.split(',')[1]), float(oi.split(',')[4])), order_items)
order_subtotals_sorted = sorted(order_subtotals)
order_subtotals_grouped = iter.groupby(order_subtotals_sorted, lambda rec: rec[0])
In [11]:
cnt = 0
for e in order_subtotals_grouped:
if cnt == 10: break
print(f'{e[0]} contains {list(e[1])}')
cnt += 1
1 contains [(1, 299.98)] 2 contains [(2, 129.99), (2, 199.99), (2, 250.0)] 4 contains [(4, 49.98), (4, 150.0), (4, 199.92), (4, 299.95)] 5 contains [(5, 99.96), (5, 129.99), (5, 299.95), (5, 299.98), (5, 299.98)] 7 contains [(7, 79.95), (7, 199.99), (7, 299.98)] 8 contains [(8, 50.0), (8, 179.97), (8, 199.92), (8, 299.95)] 9 contains [(9, 199.98), (9, 199.99), (9, 199.99)] 10 contains [(10, 21.99), (10, 99.96), (10, 129.99), (10, 199.99), (10, 199.99)] 11 contains [(11, 49.98), (11, 59.99), (11, 159.96), (11, 249.9), (11, 399.96)] 12 contains [(12, 100.0), (12, 149.94), (12, 250.0), (12, 299.98), (12, 499.95)]
In [12]:
i = [(2, 199.99), (2, 250.0), (2, 129.99)]
In [13]:
list(map(lambda rec: rec[1], i))
Out[13]:
[199.99, 250.0, 129.99]
In [14]:
sum(list(map(lambda rec: rec[1], i))) # this will go as part of first argument to starmap
Out[14]:
579.98
In [15]:
order_subtotals = map(lambda oi: (int(oi.split(',')[1]), float(oi.split(',')[4])), order_items)
order_subtotals_sorted = sorted(order_subtotals)
import itertools as iter
order_subtotals_grouped = iter.groupby(order_subtotals_sorted, lambda rec: rec[0])
list(order_subtotals_grouped)[:3]
Out[15]:
[(1, <itertools._grouper at 0x7f8b5764a940>), (2, <itertools._grouper at 0x7f8b5764a100>), (4, <itertools._grouper at 0x7f8b5764a4c0>)]
In [16]:
order_subtotals = map(lambda oi: (int(oi.split(',')[1]), float(oi.split(',')[4])), order_items)
order_subtotals_sorted = sorted(order_subtotals)
order_subtotals_grouped = iter.groupby(order_subtotals_sorted, lambda rec: rec[0])
order_revenue = iter.starmap(
lambda key, values: (key, round(sum(list(map(lambda rec: rec[1], values))), 2)),
order_subtotals_grouped
)
In [17]:
list(order_revenue)[:3]
Out[17]:
[(1, 299.98), (2, 579.98), (4, 699.85)]
{note}
Alternative solution by avoiding first map.
In [18]:
order_items_sorted = sorted(order_items, key=lambda oi: int(oi.split(',')[1]))
order_items_grouped = iter.groupby(order_items_sorted, lambda oi: int(oi.split(',')[1]))
In [19]:
order_items[1:4]
Out[19]:
['2,2,1073,1,199.99,199.99', '3,2,502,5,250.0,50.0', '4,2,403,1,129.99,129.99']
In [20]:
order_items_sorted = sorted(order_items, key=lambda oi: int(oi.split(',')[1]))
order_items_grouped = iter.groupby(order_items_sorted, lambda oi: int(oi.split(',')[1]))
cnt = 0
for e in order_items_grouped:
if cnt == 10: break
print(f'{e[0]} contains {list(e[1])}')
cnt += 1
1 contains ['1,1,957,1,299.98,299.98'] 2 contains ['2,2,1073,1,199.99,199.99', '3,2,502,5,250.0,50.0', '4,2,403,1,129.99,129.99'] 4 contains ['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'] 5 contains ['9,5,957,1,299.98,299.98', '10,5,365,5,299.95,59.99', '11,5,1014,2,99.96,49.98', '12,5,957,1,299.98,299.98', '13,5,403,1,129.99,129.99'] 7 contains ['14,7,1073,1,199.99,199.99', '15,7,957,1,299.98,299.98', '16,7,926,5,79.95,15.99'] 8 contains ['17,8,365,3,179.97,59.99', '18,8,365,5,299.95,59.99', '19,8,1014,4,199.92,49.98', '20,8,502,1,50.0,50.0'] 9 contains ['21,9,191,2,199.98,99.99', '22,9,1073,1,199.99,199.99', '23,9,1073,1,199.99,199.99'] 10 contains ['24,10,1073,1,199.99,199.99', '25,10,1014,2,99.96,49.98', '26,10,403,1,129.99,129.99', '27,10,917,1,21.99,21.99', '28,10,1073,1,199.99,199.99'] 11 contains ['29,11,365,1,59.99,59.99', '30,11,627,4,159.96,39.99', '31,11,1014,1,49.98,49.98', '32,11,191,4,399.96,99.99', '33,11,1014,5,249.9,49.98'] 12 contains ['34,12,957,1,299.98,299.98', '35,12,134,4,100.0,25.0', '36,12,1014,3,149.94,49.98', '37,12,191,5,499.95,99.99', '38,12,502,5,250.0,50.0']
In [21]:
values = order_items[1:4]
In [22]:
values
Out[22]:
['2,2,1073,1,199.99,199.99', '3,2,502,5,250.0,50.0', '4,2,403,1,129.99,129.99']
In [23]:
list(map(lambda rec: float(rec.split(',')[4]), values))
Out[23]:
[199.99, 250.0, 129.99]
In [24]:
sum(list(map(lambda rec: float(rec.split(',')[4]), values)))
Out[24]:
579.98
In [25]:
order_revenue = iter.starmap(
lambda key, values: (key, round(sum(list(map(lambda rec: float(rec.split(',')[4]), values))), 2)),
order_items_grouped
)
In [26]:
list(order_revenue)[:3]
Out[26]:
[(14, 549.94), (15, 925.91), (16, 419.93)]