Order Count by Status using itertools¶
Get count by order status using orders data set.
In [1]:
%run 02_preparing_data_sets.ipynb
In [2]:
orders[:3]
Out[2]:
['1,2013-07-25 00:00:00.0,11599,CLOSED', '2,2013-07-25 00:00:00.0,256,PENDING_PAYMENT', '3,2013-07-25 00:00:00.0,12111,COMPLETE']
In [3]:
orders_sorted = sorted(orders, key=lambda k: k.split(',')[3])
In [4]:
orders_sorted[:3]
Out[4]:
['50,2013-07-25 00:00:00.0,5225,CANCELED', '112,2013-07-26 00:00:00.0,5375,CANCELED', '527,2013-07-28 00:00:00.0,5426,CANCELED']
In [5]:
import itertools as iter
In [6]:
orders_grouped = iter.groupby(orders_sorted, lambda order: order.split(',')[3])
In [7]:
orders_grouped
Out[7]:
<itertools.groupby at 0x7fe174049860>
In [8]:
list(orders_grouped)[:3]
Out[8]:
[('CANCELED', <itertools._grouper at 0x7fe176160880>), ('CLOSED', <itertools._grouper at 0x7fe176160c10>), ('COMPLETE', <itertools._grouper at 0x7fe17404a3a0>)]
In [9]:
orders_sorted = sorted(orders, key=lambda k: k.split(',')[3])
orders_grouped = iter.groupby(orders_sorted, lambda order: order.split(',')[3])
order_count_by_status = iter.starmap(lambda key, values: (key, len(list(values))), orders_grouped)
In [10]:
order_count_by_status
Out[10]:
<itertools.starmap at 0x7fe17616f130>
In [11]:
list(order_count_by_status)
Out[11]:
[('CANCELED', 1428), ('CLOSED', 7556), ('COMPLETE', 22899), ('ON_HOLD', 3798), ('PAYMENT_REVIEW', 729), ('PENDING', 7610), ('PENDING_PAYMENT', 15030), ('PROCESSING', 8275), ('SUSPECTED_FRAUD', 1558)]