0PricingLogin
Pandas & NumPy Academy · Lesson

Aggregation Functions

Compute sum, mean, min, max, and standard deviation across an entire array or along a specific axis.

What Is Aggregation?

Aggregation boils an array down to a few summary values, like a total or an average. NumPy's built-in functions do this in fast C, no loops needed.

import numpy as np

a = np.array([3, 7, 2, 9, 1, 6])
print('sum:', a.sum())     # 28
print('mean:', a.mean())   # 4.666...
print('max:', a.max())     # 9

sum() and cumsum()

Use a.sum() for the total of every element, and np.cumsum for a running total — each spot holds the sum of everything up to it.

import numpy as np

a = np.array([1, 2, 3, 4, 5])
print(a.sum())       # 15
print(np.cumsum(a))  # [ 1  3  6 10 15]

All lessons in this course

  1. Universal Functions (ufuncs)
  2. Aggregation Functions
  3. Broadcasting Rules
  4. Boolean Masking and Fancy Indexing
← Back to Pandas & NumPy Academy