Pandas DataFrame.aggregate: Aggregate Operations on a DataFrame
Pandas DataFrame.aggregate
The DataFrame.aggregate method in pandas is used to perform aggregation operations on a DataFrame. It allows you to apply one or more aggregation functions to the rows or columns of a DataFrame. This method is an alias for DataFrame.agg.
Syntax
The syntax for DataFrame.aggregate is:
DataFrame.aggregate(func=None, axis=0, *args, **kwargs)Here, DataFrame refers to the pandas DataFrame on which the aggregation is performed.
Parameters
| Parameter | Description |
|---|---|
func | The aggregation function(s) to apply. This can be a string, function, list of functions, or dictionary mapping columns to functions. |
axis | Specifies the axis along which the aggregation is performed. Use 0 or 'index' to apply the function to each column, and 1 or 'columns' to apply the function to each row. Defaults to 0. |
*args | Positional arguments to pass to the aggregation function. |
**kwargs | Keyword arguments to pass to the aggregation function. |
Returns
A DataFrame, Series, or scalar value resulting from the aggregation operation, depending on the input parameters.
Examples
Applying a Single Aggregation Function to a DataFrame
This example demonstrates how to use aggregate to apply a single aggregation function (e.g., sum) to each column of a DataFrame.
Python Program
import pandas as pd
# Create a DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
# Apply the sum aggregation function to each column
result = df.aggregate('sum')
print(result)Output
A 6
B 15
C 24
dtype: int64Applying Multiple Aggregation Functions to a DataFrame
This example shows how to use aggregate to apply multiple aggregation functions (e.g., sum and mean) to each column of a DataFrame.
Python Program
import pandas as pd
# Create a DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
# Apply multiple aggregation functions to each column
result = df.aggregate(['sum', 'mean'])
print(result)Output
A B C
sum 6.0 15.0 24.0
mean 2.0 5.0 8.0Applying Different Aggregation Functions to Different Columns of a DataFrame
This example demonstrates how to use aggregate with a dictionary to apply different aggregation functions to different columns of a DataFrame.
Python Program
import pandas as pd
# Create a DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
# Apply different aggregation functions to different columns
result = df.aggregate({'A': 'sum', 'B': 'mean', 'C': 'max'})
print(result)Output
A 6.0
B 5.0
C 9.0
dtype: float64Applying Aggregation Functions to Rows of a DataFrame
This example shows how to use aggregate to apply aggregation functions to rows of a DataFrame by setting axis=1.
Python Program
import pandas as pd
# Create a DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
# Apply the sum aggregation function to each row
result = df.aggregate('sum', axis=1)
print(result)Output
0 12
1 15
2 18
dtype: int64Summary
In this tutorial, we explored the DataFrame.aggregate method in pandas. Key takeaways include:
- Using
aggregateto apply single or multiple aggregation functions to a DataFrame. - Applying different aggregation functions to different columns using a dictionary.
- Performing aggregation operations on rows by setting
axis=1. - Understanding that
aggregateis an alias foragg.