Aggregations using reduce¶
Let us understand how to perform global aggregations using reduce
.
We can use
reduce
on top ofiterable
to return aggregated result.It takes aggregation logic and iterable as arguments. We can pass aggregation logic either as regular function or lambda function.
reduce
returns objects of typeint
,float
etc. It is typically of type elements in the collection that is being processed.Unlike
map
andfilter
we need to importreduce
from functools.
Task 1¶
Use orders and get total number of records for a given month (201401).
Filter the data.
Perform row level transformation by changing each record to 1.
Use reduce to aggregate.
Task 2¶
Use order items data set and compute total revenue generated for a given product_id.
Filter for given product_id.
Extract order_item_subtotal for each item.
Aggregate to get the revenue for a given product id.
Note
We can also aggregate using functions such as add
, min
, max
etc to get the aggregated results.
Task 3¶
Use order items data set and get total number of items sold as well as total revenue generated for a given product_id.
Task 4¶
Create a collection with sales and commission percentage. Using that collection compute total commission amount. If the commission percent is None or not present, treat it as 0.
Each element in the collection should be a tuple.
First element is the sales amount and second element is commission percentage.
Commission for each sale can be computed by multiplying commission percentage with sales (make sure to divide commission percentage by 100).
Some of the records does not have commission percentage, in that case commission amount for that sale shall be 0
Note
Using map
function call as argument.