Get revenue details per order¶
Create a function get_revenue_per_order which takes order_items list as argument and returns a dict which contain order_item_order_id and corresponding quantity as well as order_revenue.
{note}
This is an example to get order_id and corresponding quantity as well as revenue. For each order_id, quantity and revenue are treated as list.
In [1]:
%run 07_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]:
revenue_per_order = {}
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_order_id = int(order_item.split(',')[1])
order_item_quantity = int(order_item.split(',')[3])
order_item_subtotal = float(order_item.split(',')[4])
In [7]:
order_metrics = [order_item_quantity, order_item_subtotal]
In [8]:
order_metrics
Out[8]:
[1, 299.98]
In [9]:
order_item_order_id in revenue_per_order
Out[9]:
False
In [10]:
revenue_per_order.get(order_item_order_id)
In [11]:
if revenue_per_order.get(order_item_order_id):
revenue_per_order[order_item_order_id][0] += order_metrics[0]
revenue_per_order[order_item_order_id][1] += order_metrics[1]
In [12]:
revenue_per_order
Out[12]:
{}
In [13]:
if not revenue_per_order.get(order_item_order_id):
revenue_per_order[order_item_order_id] = order_metrics
In [14]:
revenue_per_order
Out[14]:
{1: [1, 299.98]}
In [15]:
order_item = order_items[1]
In [16]:
order_item
Out[16]:
'2,2,1073,1,199.99,199.99'
In [17]:
order_item_order_id = int(order_item.split(',')[1])
order_item_quantity = int(order_item.split(',')[3])
order_item_subtotal = float(order_item.split(',')[4])
order_metrics = [order_item_quantity, order_item_subtotal]
In [18]:
order_item_order_id
Out[18]:
2
In [19]:
order_metrics
Out[19]:
[1, 199.99]
In [20]:
revenue_per_order
Out[20]:
{1: [1, 299.98]}
In [21]:
if revenue_per_order.get(order_item_order_id):
revenue_per_order[order_item_order_id][0] += order_metrics[0]
revenue_per_order[order_item_order_id][1] += order_metrics[1]
else:
revenue_per_order[order_item_order_id] = order_metrics
revenue_per_order
Out[21]:
{1: [1, 299.98], 2: [1, 199.99]}
In [22]:
order_item = order_items[2]
In [23]:
order_item
Out[23]:
'3,2,502,5,250.0,50.0'
In [24]:
order_item_order_id = int(order_item.split(',')[1])
order_item_quantity = int(order_item.split(',')[3])
order_item_subtotal = float(order_item.split(',')[4])
order_metrics = [order_item_quantity, order_item_subtotal]
In [25]:
revenue_per_order
Out[25]:
{1: [1, 299.98], 2: [1, 199.99]}
In [26]:
revenue_per_order[order_item_order_id][0] = revenue_per_order[order_item_order_id][0] + order_metrics[0]
revenue_per_order[order_item_order_id][1] = revenue_per_order[order_item_order_id][1] + order_metrics[1]
In [27]:
revenue_per_order
Out[27]:
{1: [1, 299.98], 2: [6, 449.99]}
In [28]:
def get_revenue_per_order(order_items):
revenue_per_order = {}
for order_item in order_items:
order_item_order_id = int(order_item.split(',')[1])
order_item_quantity = int(order_item.split(',')[3])
order_item_subtotal = float(order_item.split(',')[4])
order_metrics = [order_item_quantity, order_item_subtotal]
if revenue_per_order.get(order_item_order_id):
# if revenue_per_order contains key,
# then add order_item_subtotal to existing value
revenue_per_order[order_item_order_id][0] += order_metrics[0]
revenue_per_order[order_item_order_id][1] += order_metrics[1]
else:
# if revenue_per_order does not contain key,
# then add new element to revenue_per_order
revenue_per_order[order_item_order_id] = order_metrics
return revenue_per_order
In [29]:
list(get_revenue_per_order(order_items).items())[:10]
Out[29]:
[(1, [1, 299.98]), (2, [7, 579.98]), (4, [14, 699.85]), (5, [10, 1129.8600000000001]), (7, [7, 579.9200000000001]), (8, [13, 729.8399999999999]), (9, [4, 599.96]), (10, [6, 651.9200000000001]), (11, [15, 919.79]), (12, [18, 1299.8700000000001])]
{note}
This is an example to get order_id and corresponding quantity as well as revenue. For each order_id, quantity and revenue are treated as dict.
In [30]:
def get_revenue_per_order(order_items):
revenue_per_order = {}
for order_item in order_items:
order_item_order_id = int(order_item.split(',')[1])
order_item_quantity = int(order_item.split(',')[3])
order_item_subtotal = float(order_item.split(',')[4])
order_metrics = {'order_item_quantity': order_item_quantity, 'order_item_subtotal': order_item_subtotal}
if revenue_per_order.get(order_item_order_id):
# if revenue_per_order contains key,
# then add order_item_subtotal to existing value
revenue_per_order[order_item_order_id]['order_item_quantity'] += order_metrics['order_item_quantity']
revenue_per_order[order_item_order_id]['order_item_subtotal'] += order_metrics['order_item_subtotal']
else:
# if revenue_per_order does not contain key,
# then add new element to revenue_per_order
revenue_per_order[order_item_order_id] = order_metrics
return revenue_per_order
In [31]:
list(get_revenue_per_order(order_items).items())[:10]
Out[31]:
[(1, {'order_item_quantity': 1, 'order_item_subtotal': 299.98}), (2, {'order_item_quantity': 7, 'order_item_subtotal': 579.98}), (4, {'order_item_quantity': 14, 'order_item_subtotal': 699.85}), (5, {'order_item_quantity': 10, 'order_item_subtotal': 1129.8600000000001}), (7, {'order_item_quantity': 7, 'order_item_subtotal': 579.9200000000001}), (8, {'order_item_quantity': 13, 'order_item_subtotal': 729.8399999999999}), (9, {'order_item_quantity': 4, 'order_item_subtotal': 599.96}), (10, {'order_item_quantity': 6, 'order_item_subtotal': 651.9200000000001}), (11, {'order_item_quantity': 15, 'order_item_subtotal': 919.79}), (12, {'order_item_quantity': 18, 'order_item_subtotal': 1299.8700000000001})]