Pandas apply() with Lambda Examples

pandas.DataFrame.apply() can be used along with the Python lambda function to apply a custom operation to all columns in a DataFrame. A lambda function is a small anonymous function that can take any number of arguments and execute an expression.

In this article, I will explain how to use a Pandas DataFrame.apply() with lambda by examples. lambda expressions are utilized to construct anonymous functions. You can create one by using the lambda keyword.

1. Quick Examples of Pandas Apply with Lambda

If you are in a hurry, below are some quick examples of how to use the lambda function with Pandas DataFrame.apply().


# Below are some quick examples

# Example 1: Apply a lambda function to each column
df2 = df.apply(lambda x : x + 10)

# Example 2: Using Dataframe.apply() and lambda function
df["A"] = df["A"].apply(lambda x: x-2)

# Example 3: Apply function NumPy.square() to square the values of two rows 
#'A'and'B
df2 = df.apply(lambda x: np.square(x) if x.name in ['A','B'] else x)

# Example 4: Using DataFrame.map() to Single Column
df['A'] = df['A'].map(lambda A: A/2.)

# Example 5: Using DataFrame.assign() and Lambda
df2 = df.assign(B=lambda df: df.B/2)

2. DataFrame.apply() Syntax

Below is the syntax of the DataFrame.apply() method and func param is used with a lambda expression.


# Syntax of apply() function
DataFrame.apply(func, axis=0, raw=False, result_type=None, args=(), **kwargs)

Let’s create a sample DataFrame to work with some examples. Our DataFrame contains column names A, B, and C.


# Craete DataFrame
import pandas as pd
import numpy as np
data = [(3,5,7), (2,4,6),(5,8,9)]
df = pd.DataFrame(data, columns = ['A','B','C'])
print("Create DataFrame:\n", df)

Yields below output.

Pandas Apply lambda

3. Pandas Apply with Lambda to All Columns

You can apply a lambda expression for all columns of DataFrame using the apply() method, the Below example applies lambda operation i.e. adds 10 to all columns.


# Apply a lambda function to each column
df2 = df.apply(lambda x : x + 10)
print("After applying a lambda function for all columns:\n", df2)

Yields below output.

Pandas Apply lambda

4. Apply Lambda Expression to a Single Column

You can apply the lambda expression for a single column in the DataFrame. The following example subtracts every cell value by 2 for column A – df["A"]=df["A"].apply(lambda x:x-2).


# Using Dataframe.apply() and lambda function
df["A"] = df["A"].apply(lambda x: x-2)
print("After applying a lambda function for single column:\n", df)

Yields below output.


# Output:
# After applying a lambda function for single column:
   A  B  C
0  1  5  7
1  0  4  6
2  3  8  9

Similarly, you can also apply the Lambda function to all & multiple columns in Pandas, I will leave this to you to explore.

5. Using pandas.DataFrame.map() with Lambda to Single Column

Here is another alternative using the <a href="https://sparkbyexamples.com/pandas/pandas-map-function-explained/">map()</a> method along with lambda to perform operations on a single column. For example,


# Using DataFrame.map() to Single Column
df['A'] = df['A'].map(lambda A: A/2.)
print("After applying a lambda function for single column:\n", df)

Yields below output.


# Output:
# After applying a lambda function for single column:
     A  B  C
0  1.5  5  7
1  1.0  4  6
2  2.5  8  9

6. DataFrame.assign() to Apply Lambda Function

You can also try assign()function with a lambda function to perform operations on a single column.


# Using DataFrame.assign() and Lambda
df2 = df.assign(B=lambda df: df.B/2)
print("After applying a lambda function for single column:\n", df)

Yields below output.


# Output:
# After applying a lambda function for single column:
   A    B  C
0  3  2.5  7
1  2  2.0  6
2  5  4.0  9

7. Multiple columns Using NumPy.square() and Lambda Function

Apply a lambda function to multiple columns in DataFrame using Dataframe apply() along with lambda and Numpy functions.


# Apply function NumPy.square() to square the values of two rows 
'A'and'B
df2 = df.apply(lambda x: np.square(x) if x.name in ['A','B'] else x)
print("After applying a lambda function for multiple columns:\n", df)

Yields below output.


# Output:
# After applying a lambda function for multiple columns:
    A   B  C
0   9  25  7
1   4  16  6
2  25  64  9

Frequently Asked Questions of Pandas apply() with Lambda Examples

What is the basic syntax of the apply() function with a lambda function?

The syntax of the apply() function with lambda function is df['new_column'] = df['existing_column'].apply(lambda x: your_function(x))

How to apply a simple mathematical operation using apply() and lambda?

You can apply simple mathematical operations using the apply() function along with lambda. For example,df['new_column'] = df['existing_column'].apply(lambda x: x * 2)

How can I apply a function to each element of a DataFrame, not just a column?

You can apply element-wise operations using applymap() function along with lambda. for For example, df = df.applymap(lambda x: your_function(x))

How to use apply() on multiple columns?

You can use the axis parameter to apply the function row-wise (axis=1) or column-wise (axis=0). For example,
df['new_column'] = df.apply(lambda row: your_function(row['col1'], row['col2']), axis=1)

How can I apply() on a Series?

You can apply the apply() function along with lambda to a Series and do lambda operations. For example, series = series.apply(lambda x: your_function(x))

Conclusion

In this article, you have learned what is a lambda expression, and how to use the lambda function along with the apply() function to perform lambda operations on single/multiple columns. A lambda function in Python is a small anonymous function that can take any number of arguments and execute an expression. lambda expressions are utilized to construct anonymous functions. You can create one by using the lambda keyword.

Happy Learning !!

References

Naveen (NNK)

Naveen (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ @ LinkedIn

Leave a Reply

You are currently viewing Pandas apply() with Lambda Examples