# Day: March 24, 2022

## Overview of Itertools

Overview of itertools¶ Let us go through one of the important library to manipulate collections called as itertools. Functions such as filter and map are part of core Python libraries. reduce is part of functools. It is not possible to use these functions to perform advanced operations such as grouped aggregations, joins etc. Python have …

## Get Total Commission Amount

Get Total Commission Amount¶ A collection is provided with sales amount 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 is a tuple. First element is the sales amount and second element is commission percentage. Commission …

## Get Total Items Sold and Revenue

Get total items sold and revenue¶ Use order items data set and get total number of items sold as well as total revenue generated for a given product_id. Filter for items using filter related to a given product_id. Apply map to get order_item_quantity and order_item_subtotal. Use reduce to get total quantity and revenue for the …

## Get Revenue For A Given Product Id

Get Revenue for a given product_id¶ 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. In [1]: %run 02_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’] …

## Aggregations Using Reduce

Aggregations using reduce¶ Let us understand how to perform global aggregations using reduce. We can use reduce on top of iterable 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 type int, float etc. It is …

## Row Level Transformations Using Map

Row level transformations using map¶ Let us understand how we can perform row level transformations using map. Here are some of the examples. Derive new fields from existing fields. Get last 4 digits of social security number. Standardize phone numbers. Convert names to lower or upper case. Break down the address into street, city, state, …