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.
Key Points –
apply
with theaxis=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 withfor
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.
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.
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
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)
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.
you can select specific columns before applying the function. For example, selected_col = [col1, col2]
df[selected_col].apply(func, axis = 1)
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 !!
Related Articles
- Pandas Series apply() Function Usage
- Pandas apply() with Lambda Examples
- Pandas apply() Return Multiple Columns
- Pandas apply map (applymap()) Explained
- How to Slice Columns in Pandas DataFrame
- Pandas head() – Returns Top N Rows
- Pandas – Check If a Column Exists in DataFrame
- Apply Multiple Filters to Pandas DataFrame or Series
- Pandas Operator Chaining to Filter DataFrame Rows
- Pandas apply() Function to Single & Multiple Column(s)
- Pandas Difference Between map, applymap and apply Methods
References
- https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.apply.html
- https://www.w3schools.com/python/python_lambda.asp