• Post author:
  • Post category:Pandas
  • Post last modified:May 22, 2024
  • Reading time:14 mins read
You are currently viewing 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.

Advertisements

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.

Key Points –

  • Pandas apply() with lambda allows for applying custom functions to DataFrame columns or rows efficiently.
  • Lambda functions passed to apply() operate element-wise, iterating over each element in the specified axis.
  • apply() with lambda can significantly improve code readability and reduce the need for loops when working with DataFrames.
  • Understanding the performance implications and limitations of apply() with lambda is crucial for optimizing data processing workflows in Pandas.

Quick Examples of Apply with Lambda

Following are quick examples of how to use the lambda function with Pandas DataFrame.apply().


# Quick examples of apply with lambdaes

# 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)

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)

Now, let’s create a DataFrame with a few rows and columns, execute these examples, and validate results. 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

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

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.

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

DataFrame.assign() to Apply Lambda Method

The DataFrame.assign() method in pandas can be used to apply lambda functions to create new columns or modify existing ones. This method is especially useful for its ability to chain operations in a clean and readable manner. You can use assign() to modify an existing column by applying a lambda function.


# 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

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

FAQs for applying() with Lambda Function

What is apply() in pandas?

apply() is a method in pandas that allows you to apply a function along the axis of a DataFrame or Series. You can use it to apply functions to either rows or columns.

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

You can apply simple mathematical operations using the apply() function along with lambda.

How can I apply() on a Series?

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

How do I use apply() with a lambda function on a DataFrame column?

To use the apply() method with a lambda function on a DataFrame column, you need to specify the column you want to apply the function to. The lambda function will then be applied to each element of that column.

How do I use applymap() with a lambda function on a DataFrame?

The applymap() method in pandas is used to apply a function to each element of a DataFrame. This is different from apply(), which operates on columns or rows. When you want to perform element-wise operations across an entire DataFrame, applymap() is the right tool.

Conclusion

In this article, I have explained 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