Table of Contents
The combination of apply and lambda in Pandas allows users to apply a custom function to a DataFrame or Series in a concise and efficient manner. Apply allows for the application of a function to each row or column of a DataFrame, while lambda enables the creation of anonymous functions that can be used within apply. This allows for flexible and dynamic data manipulation, making it a powerful tool for data analysis and processing in Pandas.
Pandas: Use Apply & Lambda Together
You can use the following basic syntax to apply a lambda function to a pandas DataFrame:
df['col'] = df['col'].apply(lambda x: 'value1' if x < 20 else 'value2')
The following examples show how to use this syntax in practice with the following pandas DataFrame:
import pandas as pd #create DataFrame df = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'], 'points': [18, 22, 19, 14, 14, 11, 20, 28], 'assists': [5, 7, 7, 9, 12, 9, 9, 4]}) #view DataFrame print(df) team points assists 0 A 18 5 1 B 22 7 2 C 19 7 3 D 14 9 4 E 14 12 5 F 11 9 6 G 20 9 7 H 28 4
Example 1: Use Apply and Lambda to Create New Column
The following code shows how to use apply and lambda to create a new column whose values are dependent on the values of an existing column:
#create new column called 'status'
df['status'] = df['points'].apply(lambda x: 'Bad' if x < 20 else 'Good')
#view updated DataFrame
print(df)
team points assists status
0 A 18 5 Bad
1 B 22 7 Good
2 C 19 7 Bad
3 D 14 9 Bad
4 E 14 12 Bad
5 F 11 9 Bad
6 G 20 9 Good
7 H 28 4 GoodIn this example, we created a new column called status that took on the following values:
- ‘Bad‘ if the value in the points column was less than 20.
- ‘Good‘ if the value in the points column was greater than or equal to 20.
Example 2: Use Apply and Lambda to Modify Existing Column
The following code shows how to use apply and lambda to modifying an existing column in the DataFrame:
#modify existing 'points' column
df['points'] = df['points'].apply(lambda x: x/2 if x < 20 else x*2)
#view updated DataFrame
print(df)
team points assists
0 A 9.0 5
1 B 44.0 7
2 C 9.5 7
3 D 7.0 9
4 E 7.0 12
5 F 5.5 9
6 G 40.0 9
7 H 56.0 4In this example, we modified the values in the existing points column by using the following rule in the lambda function:
- If the value is less than 20, divide the value by 2.
- If the value is greater than or equal to 20, multiply the value by 2.
Using this lambda function, we were able to modify the values in the existing points column.
Additional Resources
The following tutorials explain how to perform other common functions in pandas:
Cite this article
stats writer (2024). How can I use apply and lambda together in Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-apply-and-lambda-together-in-pandas/
stats writer. "How can I use apply and lambda together in Pandas?." PSYCHOLOGICAL SCALES, 28 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-apply-and-lambda-together-in-pandas/.
stats writer. "How can I use apply and lambda together in Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-apply-and-lambda-together-in-pandas/.
stats writer (2024) 'How can I use apply and lambda together in Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-apply-and-lambda-together-in-pandas/.
[1] stats writer, "How can I use apply and lambda together in Pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I use apply and lambda together in Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
