They enable the creation of concise and efficient code, reducing the need for explicit loops and branching.
1. MAP:
Map applies a function to all items in an input list.
# Example: Converting temperatures from Celsius to Fahrenheit
celsius = [0, 10, 20, 30, 40]
fahrenheit = list(map(lambda c: (c * 9/5) + 32, celsius))
print(fahrenheit)
# Output: [32.0, 50.0, 68.0, 86.0, 104.0]
In data science, you might use map to apply a transformation to each element in a dataset.
2. FILTER:
Filter creates a list of elements for which a function returns true.
# Example: Filtering out even numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
odd_numbers = list(filter(lambda x: x % 2 != 0, numbers))
print(odd_numbers)
# Output: [1, 3, 5, 7, 9]
In data analysis, you could use filter to select data points that meet certain criteria.
3. REDUCE:
Reduce applies a rolling computation to sequential pairs of values in a list.
from functools import reduce
# Example: Calculating the product of all numbers in a list
numbers = [1, 2, 3, 4, 5]
product = reduce(lambda x, y: x * y, numbers)
print(product)
# Output: 120
In data science, reduce can be used for aggregations like sum, product, or more complex rolling computations.
Here's a more data science-oriented example combining all three:
import pandas as pd
from functools
# Sample dataset
data = pd.DataFrame({
'name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
'age': [25, 30, 35, 40, 45],
'salary': [50000, 60000, 75000, 90000, 100000]
})
# MAP: Increase all salaries by 10%
data['new_salary'] = list(map(lambda x: x * 1.1, data['salary']))
# FILTER: Select employees older than 30
older_employees = list(filter(lambda row: row['age'] > 30, data.to_dict('records')))
# REDUCE: Calculate total salary of older employees
total_salary = functools.reduce(lambda x, y: x + y['new_salary'], older_employees, 0)
print(data)
print("\nOlder employees:\n", older_employees)
print("\nTotal salary of older employees:\n", total_salary)
# Output:
name age salary new_salary
0 Alice 25 50000 55000.0
1 Bob 30 60000 66000.0
2 Charlie 35 75000 82500.0
3 David 40 90000 99000.0
4 Eve 45 100000 110000.0
Older employees:
[{'name': 'Charlie', 'age': 35, 'salary': 75000, 'new_salary': 82500.0}, {'name': 'David', 'age': 40, 'salary': 90000, 'new_salary': 99000.00000000001}, {'name': 'Eve', 'age': 45, 'salary': 100000, 'new_salary': 110000.00000000001}]
Total salary of older employees:
291500.0
This example demonstrates:
- Using MAP to apply a salary increase
- Using FILTER to select older employees
- Using REDUCE to calculate the total salary of filtered employees
These functions can significantly simplify data manipulation tasks and are often more readable and efficient than traditional for loops.
Comments
Post a Comment