Get Order Count by Status¶
Create a function get_count_by_order_status which takes orders list as argument and returns a dict which contain order_status and corresponding count.
In [2]:
%run 07_preparing_data_sets.ipynb
In [3]:
orders[:10]
Out[3]:
['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 [4]:
order = orders[0]
In [5]:
order_count = {}
In [6]:
order_status = order.split(',')[3]
In [7]:
order_status
Out[7]:
'CLOSED'
In [8]:
order_status in order_count
Out[8]:
False
In [9]:
if order_status in order_count:
order_count[order_status] += 1
In [10]:
order_count
Out[10]:
{}
In [11]:
order_status not in order_count
Out[11]:
True
In [12]:
if order_status not in order_count:
order_count[order_status] = 1
In [13]:
order_count
Out[13]:
{'CLOSED': 1}
In [14]:
order = orders[2]
In [15]:
order
Out[15]:
'3,2013-07-25 00:00:00.0,12111,COMPLETE'
In [16]:
order_status = order.split(',')[3]
In [17]:
order_status
Out[17]:
'COMPLETE'
In [18]:
order_count
Out[18]:
{'CLOSED': 1}
In [19]:
if order_status in order_count:
order_count[order_status] += 1
else:
order_count[order_status] = 1
In [20]:
order_count
Out[20]:
{'CLOSED': 1, 'COMPLETE': 1}
In [21]:
order = orders[3]
In [22]:
order
Out[22]:
'4,2013-07-25 00:00:00.0,8827,CLOSED'
In [23]:
order_status = order.split(',')[3]
In [24]:
order_status
Out[24]:
'CLOSED'
In [25]:
order_count
Out[25]:
{'CLOSED': 1, 'COMPLETE': 1}
In [26]:
if order_status in order_count:
order_count[order_status] += 1
else:
order_count[order_status] = 1
In [27]:
order_count
Out[27]:
{'CLOSED': 2, 'COMPLETE': 1}
In [28]:
def get_count_by_order_status(orders):
order_count = {} # dict
for order in orders:
order_status = order.split(',')[3]
if order_status in order_count:
# if order_count contains key,
# then increment the existing value by 1
order_count[order_status] += 1
else:
# if order_count does not contain key,
# then add new element to order_count
order_count[order_status] = 1
return order_count
In [29]:
get_count_by_order_status(orders)
Out[29]:
{'CLOSED': 7556, 'PENDING_PAYMENT': 15030, 'COMPLETE': 22899, 'PROCESSING': 8275, 'PAYMENT_REVIEW': 729, 'PENDING': 7610, 'ON_HOLD': 3798, 'CANCELED': 1428, 'SUSPECTED_FRAUD': 1558}
In [30]:
type(get_count_by_order_status(orders))
Out[30]:
dict