• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:12 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.

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.

1. 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.


# Below are the quick examples

# 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

2. 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

3. 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.

4. 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

5. 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

6. 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.

References

Happy Learning !!

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