Pandas Tutorial Pandas References

Pandas Series - agg() function



The Pandas Series agg() function is used to perform aggregation using one or more operations over the specified axis. The syntax for using this function is given below:

Syntax

Series.agg(func=None, axis=0)

Parameters

func Required. Specify function used for aggregating the data. If a function, must either work when passed a Series or when passed to Series.apply. Accepted combinations are:
  • function
  • string function name
  • list of functions and/or function names, e.g. [np.sum, 'mean']
  • dictionary of axis labels -> functions, function names or list of such.
axis Optional. Specify {0 or 'index'}. Specify axis for the function to be applied on.

Return Value

Returns following:

  • Scalar when Series.agg is called with single function.
  • Series when DataFrame.agg is called with a single function.
  • DataFrame when DataFrame.agg is called with multiple functions.

Example: using agg() on a Series

In the example below, the agg() function is applied on a Series.

import pandas as pd
import numpy as np

x = pd.Series([10, 11, 12, 9, 13, 12, 10])

print("The Series contains:")
print(x)

#applying sum 
print("\nAggregation returns:")
print(x.agg(np.sum))

The output of the above code will be:

The Series contains:
0    10
1    11
2    12
3     9
4    13
5    12
6    10
dtype: int64

Aggregation returns:
77

Example: using more operations on a Series

Multiple operations can be applied on a Series at the same time. Like in the example below, three operations - sum, mean and average are applied at the same time.

import pandas as pd
import numpy as np

x = pd.Series([10, 11, 12, 9, 13, 12, 10])

print("The Series is:")
print(x)

print("\nAggregation returns:")
print(x.agg([np.sum, np.mean, 'average']))

The output of the above code will be:

The Series is:
0    10
1    11
2    12
3     9
4    13
5    12
6    10
dtype: int64

Aggregation returns:
sum        77.0
mean       11.0
average    11.0
dtype: float64

Example: using agg() on selected series in a DataFrame

Similarly, the agg() function can be applied on selected series/column of a given DataFrame. Consider the following example.

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(5, 3),
  index = pd.date_range('1/1/2018', periods=5),
  columns = ['col1', 'col2', 'col3']
)

print("The DataFrame contains:")
print(df)

#aggregation on 'col1' series
print("\nAggregation on col1 returns:")
print(df['col1'].agg(np.sum))

The output of the above code will be:

The DataFrame contains:
                col1      col2      col3
2018-01-01  1.394185  0.632353 -0.188160
2018-01-02  0.295272 -1.299556  0.795804
2018-01-03 -1.221790 -1.423611 -1.799364
2018-01-04 -1.623139 -0.525680  1.749949
2018-01-05 -2.043086 -0.752584 -0.956120

Aggregation on col1 returns:
-3.198557633366897

❮ Pandas Series - Functions