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.
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.
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
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.
Related Articles
- Pandas apply() Function to Single & Multiple Column(s)
- Pandas apply() with Lambda Examples
- Pandas Operator Chaining to Filter DataFrame Rows
- Pandas apply map (applymap()) Explained
- Pandas apply() Return Multiple Columns
- Pandas Series apply() Function Usage
- Apply Multiple Filters to Pandas DataFrame or Series
- 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
Happy Learning !!