Use apply() function when you wanted to update every row in pandas DataFrame by calling a custom function. In order to apply a function to every row, you should use axis=1 param to apply().
By applying a function to each row, we can create a new column by using the values from the row, updating the row e.t.c.
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.
# 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)
# Using lambda function
df['new_col'] = df.apply(lambda row : row[0]+row[1]+row[2], axis=1)
# 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)
# 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.
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)
Yields below output.
# Output:
A B C
0 3 5 7
1 2 4 6
2 5 8 9
2. Apply Function to Every Row of DataFrame
By using apply() you call a function to every row of pandas DataFrame. Here the add() function will be applied to every row of pandas DataFrame. In order to iterate row by row in 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(df)
Yields below output. This creates a new column by adding values from each column of a row.
# Output:
A B C new_col
0 3 5 7 15
1 2 4 6 12
2 5 8 9 22
3. Apply Lambda to Every Row of DataFrame
Now, let’s see the same example with the lambda function. Here we are applying lambda for each row.
# Using lambda function
df['new_col'] = df.apply(lambda row : row[0]+row[1]+row[2], axis=1)
print(df)
Yields same output as above.
4. Apply Lambda Function to Update Each Row (all columns)
You can also apply a lambda
function using apply()
method, 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(df2)
Yields below output.
# Output:
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
df['new_col'] = df.apply(np.sum, axis = 1)
print(df)
Yields below output.
# Output:
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)
Conclusion
In this article, you have learned how to apply() function when you wanted to update every row in pandas DataFrame by calling a custom function. In order to apply a function to every row, you should use axis=1 param to apply(), default it uses axis=0
meaning it applies a function to each column.
By applying a function to each row, we can create a new column by using the values from the row, updating the row e.t.c.
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 !!