• Post author:
  • Post category:Pandas
  • Post last modified:December 10, 2024
  • Reading time:14 mins read
You are currently viewing Pandas Apply Function to Every Row

Use the apply() function when you want to update every row in the Pandas DataFrame by calling a custom function. In order to apply a function to every row, you should use the axis=1 param to the apply() function.

Advertisements

By applying a function to each row, we can create a new column by using the values from the row, updating the row, etc.

Note that by default it uses axis=0 meaning it applies a function to each column.

Key Points –

  • apply with the axis=1 parameter allows you to apply a function to each row, enabling operations based on row data.
  • You can pass a lambda function to apply for concise, on-the-fly operations without needing to define a separate function.
  • Use apply for complex row-based calculations that go beyond basic arithmetic, allowing functions with multiple steps or conditions.
  • apply is useful for data transformations that depend on values from multiple columns in a row.
  • apply can return various data types, including single values, lists, or even Series, allowing flexibility in the output structure.
  • Using apply with row operations is typically more efficient than looping through rows manually with for loops.

Quick Examples of Pandas Apply Function to Every Row

Below are some quick examples of how to apply a function to every row of pandas DataFrame.


# Quick examples of pandas apply function to every row

# Example 1: Using Dataframe.apply() 
# To apply function to every row
def add(row):
   return row[0]+row[1]+row[2]

df['new_col'] = df.apply(add, axis=1)

# Example 2: Pandas apply function to every row 
# Using lambda function
df['new_col'] = df.apply(lambda row : row[0]+row[1]+row[2], axis=1)

# Example 3: Add 3 to each column of a row 
df2 = df.apply(lambda row : pd.Series([row[0]+3,row[1]+3,row[2]+3]), axis=1)

# Example 4: Apply function NumPy.sum() to each row
df['new_col'] = df.apply(np.sum, axis = 1)

Let’s create a sample DataFrame to work with some examples.


# Create a 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 row

Use the apply() Function to Every Row of DataFrame

By using apply() function you can call a function to every row of pandas DataFrame. Here the add() function will be applied to every row of the Pandas DataFrame. In order to iterate row by row in the apply() function use axis=1.


# Using Dataframe.apply() to apply function 
# To every row
def add(row):
   return row[0]+row[1]+row[2]

df['new_col'] = df.apply(add, axis=1)
print("Use the apply() function to every row:\n", df)

Yields below output. This creates a new column by adding values from each column of a row.

pandas apply function row

Apply Lambda to Every Row of DataFrame

You can use the apply() function along with a lambda function to apply a specific operation to every row of a Pandas DataFrame.


# Using lambda function
df['new_col'] = df.apply(lambda row : row[0]+row[1]+row[2], axis=1)
print("Use the apply() function to every row:\n", df)

Yields the same output as above.

Apply Lambda Function to Update Each Row (all columns)

To apply a lambda function along with the apply() method to update each row in a DataFrame. The below example adds 3 to all column values for each row.


# Add 3 to each column of a row 
df2 = df.apply(lambda row : pd.Series([row[0]+3,row[1]+3,row[2]+3]), axis=1)
print("Use the apply() function to every row:\n", df2)

Yields below output.


# Output:
# Use the apply() function to every row:
   0   1   2
0  6   8  10
1  5   7   9
2  8  11  12

Apply NumPy.sum() to Every Row

Finally, let’s see how to apply a NumPy function to each row.


# Apply function NumPy.sum() to each row
import pandas as pd
import numpy as np
df['new_col'] = df.apply(np.sum, axis = 1)
print("Use the apply() function to every row:\n", df)

Yields below output.


# Output:
# Use the apply() function to every row:
    A   B  C
0   9  25  7
1   4  16  6
2  25  64  9

Complete Example


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(df)

# Using Dataframe.apply() 
# To apply function to every row
def add(row):
   return row[0]+row[1]+row[2]

df['new_col'] = df.apply(add, axis=1)
print(df)

# Using lambda function
df['new_col'] = df.apply(lambda row : row[0]+row[1]+row[2], axis=1)
print(df)

# Add 3 to each column of a row 
df2 = df.apply(lambda row : pd.Series([row[0]+3,row[1]+3,row[2]+3]), axis=1)
print(df2)

# Apply function NumPy.sum() to each row
df['new_col'] = df.apply(np.sum, axis = 1)
print(df)

Frequently Asked Questions of Pandas Apply Function to Every Row

How does the apply() function work on every row of a DataFrame?

When applying an apply() function to every row of a DataFrame, you can set the axis parameter as 1, i.e. the function should be applied along the columns (each row). For example, df.apply(func, axis=1)

What type of function can be passed to the apply() method for rows?

You can pass any function that takes a single argument (a row in this case) and returns a value. This can be a built-in Python function, a lambda function, or a custom function you define.

How can I apply a function to a subset of columns in each row?

you can select specific columns before applying the function. For example, selected_col = [col1, col2]
df[selected_col].apply(func, axis = 1)

How can I use the apply function on a Series as well?

The apply function can be used on both DataFrames and Series. When used on a Series, it applies the function element-wise. For example, series.apply(func)

Conclusion

In this article, you have learned how to use the apply() function when you want to update every row in Pandas DataFrame by calling a custom function. In order to apply this function to every row, you should use the axis=1 param and use axis=0 for each column.

Happy Learning !!

References