Lambda functions are often a convenient way to write throw away functions on the fly. If you need to write a more complicated function you may still need to use the more formal def
method, but lambda functions provide a quick and concise way to write functions.
You will be able to:
- Understand what lambda functions are and why they are useful
- Use lambda functions to transform data within lists and DataFrames
Let's say you want to count the number of words in each yelp review.
import pandas as pd
df = pd.read_csv('Yelp_Reviews.csv')
df.head(2)
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
Unnamed: 0 | business_id | cool | date | funny | review_id | stars | text | useful | user_id | |
---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | pomGBqfbxcqPv14c3XH-ZQ | 0 | 2012-11-13 | 0 | dDl8zu1vWPdKGihJrwQbpw | 5 | I love this place! My fiance And I go here atl... | 0 | msQe1u7Z_XuqjGoqhB0J5g |
1 | 2 | jtQARsP6P-LbkyjbO1qNGg | 1 | 2014-10-23 | 1 | LZp4UX5zK3e-c5ZGSeo3kA | 1 | Terrible. Dry corn bread. Rib tips were all fa... | 3 | msQe1u7Z_XuqjGoqhB0J5g |
df['text'].map(lambda x: len(x.split())).head()
0 58
1 30
2 30
3 82
4 32
Name: text, dtype: int64
Similar to defining functions in general or naming the iterable in for loops, the variable that you use after calling the lambda
keyword does not matter
df['text'].map(lambda review_text: len(review_text.split())).head()
0 58
1 30
2 30
3 82
4 32
Name: text, dtype: int64
Lambda functions can also accept some conditionals if chained in a list comprehension
df['text'].map(lambda x: 'Good' if any([word in x.lower() for word in ['awesome', 'love', 'good', 'great']]) else 'Bad').head()
0 Good
1 Bad
2 Good
3 Bad
4 Bad
Name: text, dtype: object
The above is terribly poor style and does in no way represent pep-8 or pythonic style. (For example, no line should be over 72 characters according to pep-8; the previous line was 127 characters.) That said, it is an interesting demonstration of chaining a conditional, the any method and a list comprehension all inside a lambda function!
Shew!
Perhaps we want to naively select the year from the date string rather then convert it to a datetime object.
df.date.map(lambda x: x[:4]).head()
0 2012
1 2014
2 2014
3 2011
4 2016
Name: date, dtype: object
#Without a key
names = ['Miriam Marks','Sidney Baird','Elaine Barrera','Eddie Reeves','Marley Beard',
'Jaiden Liu','Bethany Martin','Stephen Rios','Audrey Mayer','Kameron Davidson',
'Carter Wong','Teagan Bennett']
sorted(names)
['Audrey Mayer',
'Bethany Martin',
'Carter Wong',
'Eddie Reeves',
'Elaine Barrera',
'Jaiden Liu',
'Kameron Davidson',
'Marley Beard',
'Miriam Marks',
'Sidney Baird',
'Stephen Rios',
'Teagan Bennett']
#Sorting by last name
names = ['Miriam Marks','Sidney Baird','Elaine Barrera','Eddie Reeves','Marley Beard',
'Jaiden Liu','Bethany Martin','Stephen Rios','Audrey Mayer','Kameron Davidson',
'Teagan Bennett']
sorted(names, key=lambda x: x.split()[1])
['Sidney Baird',
'Elaine Barrera',
'Marley Beard',
'Teagan Bennett',
'Kameron Davidson',
'Jaiden Liu',
'Miriam Marks',
'Bethany Martin',
'Audrey Mayer',
'Eddie Reeves',
'Stephen Rios']
Lambda functions can be a convenient way to write "throw away" functions that you want to declare inline. In the next lesson we'll give you some practice with creating them!