import pandas as pd
Exploring Lambda Functions in Python
Lambda functions in Python are small, anonymous functions that can take any number of arguments but can only have one expression. They are ideal for situations where a small, temporary function is needed without the overhead of defining a formal function. They offer a quick and concise way to create small, reusable pieces of code directly within code, particularly in functional programming paradigms, enhancing readability and conciseness.
Syntax:
lambda arguments: expression
Arguments: Arguments in a lambda function represent the input values or variables on which the function performs its operation.
Expression: Expression in a lambda function is the computation or operation performed on the input arguments, producing the result of the function.
Importing Library
Importing Dataset
=pd.read_csv("Dataset.csv")
df df.head()
Row ID | Order Date | Ship Mode | Customer ID | Postal Code | Category | Sub-Category | Product Name | Sales | |
---|---|---|---|---|---|---|---|---|---|
0 | 4010 | 01-01-18 | Standard Class | SC-20725 | 90036.0 | Furniture | Furnishings | Howard Miller 11-1/2" Diameter Brentwood Wall ... | 474.430 |
1 | 8070 | 01-01-18 | Standard Class | JM-15250 | 77340.0 | Office Supplies | Storage | SAFCO Boltless Steel Shelving | 454.560 |
2 | 8071 | 01-01-18 | Standard Class | JM-15250 | 77340.0 | Furniture | Furnishings | Tenex Carpeted, Granite-Look or Clear Contempo... | 141.420 |
3 | 8072 | 01-01-18 | Standard Class | JM-15250 | 77340.0 | Furniture | Chairs | Office Star - Contemporary Task Swivel Chair | 310.744 |
4 | 8073 | 01-01-18 | Standard Class | JM-15250 | 77340.0 | Office Supplies | Art | Fluorescent Highlighters by Dixon | 12.736 |
Let’s try out few examples using lambda function:
Using Lambda Function as Filter for Data Selection
=df.loc[lambda x: x['Category'] == 'Furniture']
cat_furniture cat_furniture.head()
Row ID | Order Date | Ship Mode | Customer ID | Postal Code | Category | Sub-Category | Product Name | Sales | |
---|---|---|---|---|---|---|---|---|---|
0 | 4010 | 01-01-18 | Standard Class | SC-20725 | 90036.0 | Furniture | Furnishings | Howard Miller 11-1/2" Diameter Brentwood Wall ... | 474.430 |
2 | 8071 | 01-01-18 | Standard Class | JM-15250 | 77340.0 | Furniture | Furnishings | Tenex Carpeted, Granite-Look or Clear Contempo... | 141.420 |
3 | 8072 | 01-01-18 | Standard Class | JM-15250 | 77340.0 | Furniture | Chairs | Office Star - Contemporary Task Swivel Chair | 310.744 |
8 | 849 | 01-01-18 | Standard Class | GA-14725 | 44052.0 | Furniture | Furnishings | Linden 10" Round Wall Clock, Black | 48.896 |
14 | 8363 | 02-01-18 | Second Class | TS-21655 | 79907.0 | Furniture | Tables | Bevis Oval Conference Table, Walnut | 913.430 |
Using Lambda Function to Categorize Data Based on Conditions
'Sales_Category']=df['Sales'].apply(lambda Sales: 'Very High Sales' if Sales > 10000 else('High Sales'
df[if Sales > 5000 else 'Low Sales'))
df.head()
Row ID | Order Date | Ship Mode | Customer ID | Postal Code | Category | Sub-Category | Product Name | Sales | Sales_Category | |
---|---|---|---|---|---|---|---|---|---|---|
0 | 4010 | 01-01-18 | Standard Class | SC-20725 | 90036.0 | Furniture | Furnishings | Howard Miller 11-1/2" Diameter Brentwood Wall ... | 474.430 | Low Sales |
1 | 8070 | 01-01-18 | Standard Class | JM-15250 | 77340.0 | Office Supplies | Storage | SAFCO Boltless Steel Shelving | 454.560 | Low Sales |
2 | 8071 | 01-01-18 | Standard Class | JM-15250 | 77340.0 | Furniture | Furnishings | Tenex Carpeted, Granite-Look or Clear Contempo... | 141.420 | Low Sales |
3 | 8072 | 01-01-18 | Standard Class | JM-15250 | 77340.0 | Furniture | Chairs | Office Star - Contemporary Task Swivel Chair | 310.744 | Low Sales |
4 | 8073 | 01-01-18 | Standard Class | JM-15250 | 77340.0 | Office Supplies | Art | Fluorescent Highlighters by Dixon | 12.736 | Low Sales |
Using Lambda Function for Aggregating Summary Statistics
= df.groupby("Customer ID").apply(lambda x: pd.Series([x["Sales"].sum(), x["Sales"].count()],
total_sales =["Total Sales", "Count of Sales per Customer"]))
index total_sales.head()
Total Sales | Count of Sales per Customer | |
---|---|---|
Customer ID | ||
AA-10315 | 374.480 | 2.0 |
AA-10375 | 206.732 | 5.0 |
AA-10480 | 15.552 | 1.0 |
AA-10645 | 12.960 | 1.0 |
AB-10060 | 2936.264 | 8.0 |
Using Lambda Function in a For Loop
= {}
total_sales_per_category
# Calculating Total Sales for Each Product Category
for category, category_data in df.groupby('Category'):
= round(sum(map(lambda x: x[1], category_data['Sales'].items())),2)
total_sales_per_category[category]
total_sales_per_category
{'Furniture': 212313.79, 'Office Supplies': 240363.94, 'Technology': 269370.69}
Conclusion:
Lambda functions represent a key feature of Python’s expressive syntax, enabling developers to write compact and efficient code for a wide range of tasks. Their simplicity and flexibility make them invaluable for tasks such as data manipulation, sorting, filtering, and more. By embracing lambda functions, Python programmers can embrace a more functional style of programming and enhance their productivity and code maintainability.