• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:16 mins read
You are currently viewing Pandas apply() Function to Single & Multiple Column(s)

Using Pandas.DataFrame.apply() method you can execute a function to a single column, all, and a list of multiple columns (two or more). In this article, I will cover how to apply() a function on values of a selected single, multiple, and all columns. For example, let’s say we have three columns and would like to apply a function on a single column without touching the other two columns and return a DataFrame with three columns.

1. Quick Examples of Pandas Apply Function to a Column

If you are in a hurry, below are some quick examples of how to apply a function to single and multiple columns (two or more) in Pandas DataFrame.


# Below are some quick examples

# Example 1: Using Dataframe.apply() to apply function add column
def add_3(x):
   return x+3
df2 = df.apply(add_3)


# Example 2: Using apply function single column
def add_4(x):
   return x+4
df["B"] = df["B"].apply(add_4)


# Example 3: Apply to multiple columns
df[['A','B']] = df[['A','B']].apply(add_3)


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


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


# Example 6: Using Dataframe.apply() & [] operator
df['A'] = df['A'].apply(np.square)


# Example 7: Using numpy.square() and [] operator
df['A'] = np.square(df['A'])


# Example 8: 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 9: Apply function single column using transform() 
def add_2(x):
    return x+2
df = df.transform(add_2)

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

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

2. Syntax of Pandas.DataFrame.apply() Function

If you are a learner let’s see the syntax of the apply() method and execute some examples of how to apply it on a single column, multiple, and all columns.

Below is a syntax of pandas.DataFrame.apply()


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

Let’s create a sample DataFrame with column names A, B, and C.


# Create 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 Function

3. Pandas Apply Function to Single Column

You can define a function add_4() that adds value 4 for every value in a specified column value and uses this on the apply() function. To apply it to a single column, qualify the column name using df["col_name"]. The example below applies a function to a column B.


# Using apply function single column
def add_4(x):
   return x+4
df["B"] = df["B"].apply(add_4)
print("After applying a function on a single column:\n", df)

Yields below output. This applies the function to every row in DataFrame for a specified column.

Pandas Apply Function

4. Pandas Apply Function to All Columns

In some cases we would want to apply a function on all pandas columns, you can do this using the apply() function. Here the add_3() function will be applied to all DataFrame columns.


# Using Dataframe.apply() to apply function add column
def add_3(x):
   return x+3
df2 = df.apply(add_3)
print("After applying a function on multiple columns:\n", df2)

Yields below output.


# Output:
# After applying a function on multiple columns:
   A   B   C
0  6   8  10
1  5   7   9
2  8  11  12

5. Pandas Apply Function to Lists of Columns

Similarly, you can apply a function on a selected list of columns. In this case, this function will apply to only the selected list of columns without touching the rest of the columns.


# Apply() function on selected list of multiple columns
df = pd.DataFrame(data, columns = ['A','B','C'])
df[['A','B']] = df[['A','B']].apply(add_3)
print("After applying a function on list of columns:\n", df)

Yields below output


# Output:
# After applying a function on list of columns:
   A   B  C
0  6   8  7
1  5   7  6
2  8  11  9

6. Apply the Lambda Function to Each Column

You can also use a lambda expression with the apply() method to manipulate each column value in a DataFrame, the Below example, adds 10 to all column values.


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

Yields below output.


# Output:
# After applying a lambda function:
    A   B   C
0  13  15  17
1  12  14  16
2  15  18  19

7. Apply the Lambda Function to a Single Column

You can apply the lambda function 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 to a single column:\n", df)

Yields below output.


# Output:
# After applying a lambda function to a 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.

8. Using pandas.DataFrame.transform() to Apply Function Column

Using DataFrame.apply() method & lambda functions the resultant DataFrame can be any number of columns whereas with transform() function the resulting DataFrame must have the same length as the input DataFrame.


# Using DataFrame.transform() 
def add_2(x):
    return x+2
df = df.transform(add_2)
print("After applying a function to every column:\n", df)

Yields below output.


# Output:
# After applying a function to every column:
   A   B   C
0  5   7   9
1  4   6   8
2  7  10  11

9. Using pandas.DataFrame.map() to Single Column

Here is another alternative using map() method.


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

Yields below output.


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

10. DataFrame.assign() to Apply Lambda Function

You can also try assign()


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

Yields below output.


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

11. Using the Numpy function on a single Column

Use df['A']=df['A'].apply(np.square) to select the column from DataFrame as a series using the [] operator and apply NumPy.square() method.


# Using Dataframe.apply() & [] operator
df['A'] = df['A'].apply(np.square)
print("After applying a function to a column:\n", df)

Yields below output.


# Output:
# After applying a function to a column:
    A  B  C
0   9  5  7
1   4  4  6
2  25  8  9

12. Using NumPy.square() Method

You can also do the same without using the apply() function and directly using Numpy.

 
# Using numpy.square() and [] operator
df['A'] = np.square(df['A'])
print("After applying a function to a column:\n", df)
 

Yields the same output as above.

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

Apply a lambda function to multiple columns in DataFrame using Dataframe apply(), 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 to multiple columns:\n", df2)

Yields below output.


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

Frequently Asked Questions on Pandas apply() Function

What is the apply() function in Pandas?

apply() is a Pandas DataFrame method used to apply a function along the axis of a DataFrame. It can be used to perform operations on both rows and columns.

How does the apply() function work on a single column?

When applied to a single column, apply() applies the specified function to each element in that column. For example, df['column'] = df['column'].apply(lambda x: x * 2)

How can I use apply() it on multiple columns?

you can use apply() on multiple columns by specifying the axis parameter. When applying to columns, use axis=0. For example, df[['column1', 'column2']] = df[['column1', 'column2']].apply(lambda x: x * 2, axis=0)

How can I apply custom functions with apply()?

You can use any custom function with apply(). Just define your function and pass it as an argument. For example, def add_3(x):<br/> return x+3<br/> df2 = df.apply(add_3)

Conclusion

In this article, you have learned how to apply a function to a single column, all, and multiple columns (two or more) of Pandas DataFrame using apply(), transform(), NumPy.square(), map(), transform(), and assign() methods.

Happy Learning !!

References

Naveen Nelamali

Naveen Nelamali (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 and Medium

Leave a Reply