The .apply() method in Pandas is used to apply a function to each row or column of a DataFrame. It is a very versatile method that allows you to perform complex operations on your data with just a few lines of code.
The basic syntax of the .apply() method is as follows:
df.apply(func, axis=0, args=(), **kwargs) |
Here, func is the function you want to apply, axis specifies whether you want to apply the function row-wise (axis=0) or column-wise (axis=1), args is a tuple of additional arguments to pass to the function, and **kwargs are any additional keyword arguments to pass to the function.
For example, let's say we have a DataFrame of student grades:
import pandas as pdgrades = pd.DataFrame({'name': ['Alice', 'Bob', 'Charlie'], 'math': [80, 90, 70], 'science': [75, 85, 80], 'english': [90, 85, 75]}) |
We can use the .apply() method to calculate the average grade for each student:
def average(row): return row.mean()grades['average'] = grades.apply(average, axis=1)print(grades) |
Output:
name math science english average0 Alice 80 75 90 81.671 Bob 90 85 85 86.672 Charlie 70 80 75 75.00 |
In this example, we define a function average() that takes a row of the DataFrame as input and returns the mean of the row. We then use the .apply() method to apply this function to each row of the DataFrame using axis=1. The resulting averages are added to a new column called 'average' in the original DataFrame.