Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Apply function to every row in a Pandas DataFrame in Python
In this tutorial, we will learn how to apply functions to every row in a Pandas DataFrame using the apply() method. This is useful when you need to perform calculations across columns or transform data row-wise.
The apply() Method
The apply() method is used to apply a function along an axis of a DataFrame. When axis=1, it applies the function to each row. This is particularly useful for creating new columns based on calculations involving multiple existing columns.
Using Custom Functions with Lambda
You can apply custom functions to each row using lambda expressions ?
import pandas as pd
# Function to multiply two values
def multiply(x, y):
return x * y
# Creating a DataFrame
data = {
'Maths': [10, 34, 53],
'Programming': [23, 12, 43]
}
df = pd.DataFrame(data)
print('--------------------Before------------------')
print(df)
print()
# Applying the multiply function to each row
df['Multiply'] = df.apply(lambda row: multiply(row['Maths'], row['Programming']), axis=1)
print('--------------------After------------------')
print(df)
--------------------Before------------------ Maths Programming 0 10 23 1 34 12 2 53 43 --------------------After------------------ Maths Programming Multiply 0 10 23 230 1 34 12 408 2 53 43 2279
Using Built-in Functions
You can also use built-in Python functions like sum(), max(), or min() directly ?
import pandas as pd
# Creating a DataFrame
data = {
'Maths': [10, 34, 53],
'Programming': [23, 12, 43]
}
df = pd.DataFrame(data)
print('--------------------Before------------------')
print(df)
print()
# Using built-in sum function
df['Sum'] = df.apply(sum, axis=1)
print('--------------------After------------------')
print(df)
--------------------Before------------------ Maths Programming 0 10 23 1 34 12 2 53 43 --------------------After------------------ Maths Programming Sum 0 10 23 33 1 34 12 46 2 53 43 96
Using NumPy Functions
NumPy functions can also be applied to DataFrame rows for more advanced mathematical operations ?
import pandas as pd
import numpy as np
# Creating a DataFrame
data = {
'Maths': [10, 34, 53],
'Programming': [23, 12, 43]
}
df = pd.DataFrame(data)
print('--------------------Before------------------')
print(df)
print()
# Using NumPy functions
df['Mean'] = df.apply(np.mean, axis=1)
df['Max'] = df.apply(np.max, axis=1)
print('--------------------After------------------')
print(df)
--------------------Before------------------ Maths Programming 0 10 23 1 34 12 2 53 43 --------------------After------------------ Maths Programming Mean Max 0 10 23 16.5 23 1 34 12 23.0 34 2 53 43 48.0 53
Key Parameters
The most important parameter for row-wise operations is axis=1. Here's what it means:
-
axis=0(default): Apply function along columns (down the rows) -
axis=1: Apply function along rows (across the columns)
Conclusion
The apply() method with axis=1 is a powerful way to apply functions to every row in a Pandas DataFrame. You can use custom functions, built-in Python functions, or NumPy functions to perform row-wise calculations and create new columns efficiently.
