Get order count by month¶
Create a function get_order_count_by_month which takes orders list and order_status as arguments and returns a dict which contain order_month and count.
- We only have to get count those orders which belong to the passed order_status.
- If order_status is not passed then we need to count all records.
In [1]:
%run 07_preparing_data_sets.ipynb
In [2]:
orders[:10]
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', '4,2013-07-25 00:00:00.0,8827,CLOSED', '5,2013-07-25 00:00:00.0,11318,COMPLETE', '6,2013-07-25 00:00:00.0,7130,COMPLETE', '7,2013-07-25 00:00:00.0,4530,COMPLETE', '8,2013-07-25 00:00:00.0,2911,PROCESSING', '9,2013-07-25 00:00:00.0,5657,PENDING_PAYMENT', '10,2013-07-25 00:00:00.0,5648,PENDING_PAYMENT']
In [3]:
order = orders[0]
In [4]:
order
Out[4]:
'1,2013-07-25 00:00:00.0,11599,CLOSED'
In [5]:
order.split(',')[1][:7]
Out[5]:
'2013-07'
In [6]:
def get_order_count_by_month(orders, order_status='ALL'):
order_count = {}
for order in orders:
order_month = order.split(',')[1][:7]
l_order_status = order.split(',')[3]
if order_status == 'ALL' or l_order_status == order_status:
if order_month in order_count:
order_count[order_month] += 1
else:
order_count[order_month] = 1
return order_count
In [7]:
get_order_count_by_month(orders)
Out[7]:
{'2013-07': 1533, '2013-08': 5680, '2013-09': 5841, '2013-10': 5335, '2013-11': 6381, '2013-12': 5892, '2014-01': 5908, '2014-02': 5635, '2014-03': 5778, '2014-04': 5657, '2014-05': 5467, '2014-06': 5308, '2014-07': 4468}
In [8]:
get_order_count_by_month(orders, 'COMPLETE')
Out[8]:
{'2013-07': 515, '2013-08': 1880, '2013-09': 1933, '2013-10': 1783, '2013-11': 2141, '2013-12': 1898, '2014-01': 1911, '2014-02': 1869, '2014-03': 1967, '2014-04': 1932, '2014-05': 1854, '2014-06': 1797, '2014-07': 1419}
In [9]:
get_order_count_by_month(orders, 'CLOSED')
Out[9]:
{'2013-07': 161, '2013-08': 637, '2013-09': 676, '2013-10': 609, '2013-11': 686, '2013-12': 705, '2014-01': 633, '2014-02': 602, '2014-03': 612, '2014-04': 583, '2014-05': 585, '2014-06': 563, '2014-07': 504}