Pandas DataFrame.agg: Aggregate Operations on a DataFrame
Pandas DataFrame.agg
The DataFrame.agg
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.
Syntax
The syntax for DataFrame.agg
is:
DataFrame.agg(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 agg
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.agg('sum')
print(result)
Output
A 6
B 15
C 24
dtype: int64
Applying Multiple Aggregation Functions to a DataFrame
This example shows how to use agg
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.agg(['sum', 'mean'])
print(result)
Output
A B C
sum 6.0 15.0 24.0
mean 2.0 5.0 8.0
Applying Different Aggregation Functions to Different Columns of a DataFrame
This example demonstrates how to use agg
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.agg({'A': 'sum', 'B': 'mean', 'C': 'max'})
print(result)
Output
A 6.0
B 5.0
C 9.0
dtype: float64
Applying Aggregation Functions to Rows of a DataFrame
This example shows how to use agg
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.agg('sum', axis=1)
print(result)
Output
0 12
1 15
2 18
dtype: int64
Summary
In this tutorial, we explored the DataFrame.agg
method in pandas. Key takeaways include:
- Using
agg
to 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
.