Aggregate DataFrame using Operation(s) over an Axis
To aggregate a DataFrame using one or more operations over the specified axis, call pandas.DataFrame.agg() method and pass the required values for parameters.
In this tutorial, we will learn the syntax of DataFrame.agg() method and how to use this function to aggregate this DataFrame using specified operation(s) and axis.
Syntax
The syntax of pandas DataFrame.agg() method is
DataFrame.agg(func=None, axis=0, *args, **kwargs)
where
Parameter | Value | Description |
---|---|---|
func | function, str, list or dict | Function to use for aggregating the data. If a function, must either work when passed a DataFrame or when passed to DataFrame.apply. Accepted combinations are: function string function name list of functions and/or function names. Examples: sum, ‘sum’, [sum, max, min], etc. |
axis | {0 or ‘index’, 1 or ‘columns’}, default 0 | If 0 or ‘index’: apply function to each column. If 1 or ‘columns’: apply function to each row. |
*args | Positional arguments to pass to func. | |
**kwargs | Keyword arguments to pass to func. |
Return Value
The return value depends on the number of functions passed for func
parameter.
- Series : when DataFrame.agg() is called with a single function.
- DataFrame : when DataFrame.agg() is called with several functions.
Examples
Aggregate DataFrame with Sum Operation along Columns Axis
In the following program, we take a DataFrame and aggregate it using the sum function with axis = ‘columns’.
Example.py
import pandas as pd
df = pd.DataFrame(
{'col_1': [10, 20, 30], 'col_2': [40, 50, 60]})
result = df.agg(sum, axis='columns')
print(result)
Output
0 50
1 70
2 90
dtype: int64
Aggregate DataFrame along Index Axis
In the following program, we take a DataFrame and aggregate it using the sum function with axis = ‘index’.
Example.py
import pandas as pd
df = pd.DataFrame(
{'col_1': [10, 20, 30], 'col_2': [40, 50, 60]})
result = df.agg(sum, axis='index')
print(result)
Output
col_1 60
col_2 150
dtype: int64
Aggregate DataFrame with Multiple Operations
We can also aggregate a DataFrame with multiple functions. In the following program, we pass a list of functions for the func
parameter.
Example.py
import pandas as pd
df = pd.DataFrame(
{'col_1': [10, 20, 30], 'col_2': [40, 50, 60]})
result = df.agg([sum, max], axis='index')
print(result)
Output
col_1 col_2
sum 60 150
max 30 60
Conclusion
In this Pandas Tutorial, we learned how to find the aggregate of a DataFrame with specified function(s) and axis, using pandas DataFrame.agg() method.