100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Python

Aggregations and Statistics

NumPy provides fast, axis-aware aggregation functions like sum, mean, std, and var, plus NaN-safe variants for handling missing values in numerical data.

NumPy OperationsIntermediate9 min readJul 8, 2026
Analogies

Aggregations and Statistics

NumPy's aggregation functions — sum, mean, std, var, min, max, argmin, argmax, median, percentile — reduce an array (or one axis of it) to a smaller result, and they run as compiled, vectorized operations that vastly outperform manual Python loops. Every one of them accepts an axis parameter to control which dimension is collapsed, and understanding this parameter is the key to using them correctly on multi-dimensional data such as tabular datasets shaped as (rows, columns).

🏏

Cricket analogy: np.mean and np.max on a season's scoring array reduce hundreds of ball-by-ball entries to one number, and specifying axis lets you collapse by innings versus by player when the data is shaped as (players, innings).

Variance, Standard Deviation, and ddof

np.var() and np.std() compute population statistics by default (dividing by N, the number of observations), which is correct when your data represents the entire population. For a sample intended to estimate a population parameter, you typically want the sample (unbiased) variant, which divides by N - 1 instead of N — set ddof=1 (delta degrees of freedom) to get this. Forgetting ddof=1 is a very common source of subtly wrong statistics when working with sampled data, especially since pandas defaults to ddof=1 while NumPy defaults to ddof=0, a frequent source of confusion when values don't match between the two libraries.

🏏

Cricket analogy: Computing np.std on a bowler's economy rates across a full season (population) divides by N, but estimating the standard deviation from a sample of just five matches to infer season-wide consistency needs ddof=1 for an unbiased estimate.

Handling NaN Values in Aggregations

Standard aggregation functions propagate NaN: if any element in the reduced axis is NaN, the result is NaN. NumPy provides NaN-aware counterparts — np.nansum, np.nanmean, np.nanstd, np.nanmedian — that ignore NaN values during the computation, which is essential when working with real-world data containing missing values. These functions are the NumPy-level building blocks that pandas' own missing-data-aware aggregations rely on internally.

🏏

Cricket analogy: If a rain-affected match leaves one innings' score as NaN, np.sum over the season propagates that NaN into the total, so np.nansum is needed to get a meaningful season total that ignores the missing match.

python
import numpy as np

data = np.array([[85, 90, np.nan], [70, 88, 95], [60, np.nan, 75]])

# Standard mean propagates NaN
print(np.mean(data, axis=0))     # [ 71.67  89.    85. ] -- wait, NaN propagates per column with NaN present
# actual: columns with a NaN produce NaN: [71.66666667         nan         nan]

# NaN-aware mean ignores missing values
print(np.nanmean(data, axis=0))  # [71.66666667 89.         85.        ]

# Population vs sample standard deviation
scores = np.array([88, 92, 79, 85, 91])
print(scores.std())          # ddof=0 (population), divides by N
print(scores.std(ddof=1))    # ddof=1 (sample), divides by N-1 -- matches pandas' default

# argmax/argmin return the INDEX of the extreme value
print(scores.argmax())       # 1 (index of 92)

pandas' .std() and .var() default to ddof=1 (sample statistics), while NumPy's .std() and .var() default to ddof=0 (population statistics) -- this mismatch is one of the most common sources of 'why don't these numbers match' confusion when moving between the two libraries.

np.mean(data) on an array containing any NaN returns NaN for the whole reduction (or that slice, with axis specified) -- always reach for np.nanmean/np.nansum/np.nanstd when your data may contain missing values, or filter/impute first.

  • Aggregation functions (sum, mean, std, var, min, max) accept an axis parameter to control which dimension collapses.
  • np.std/np.var default to ddof=0 (population); pass ddof=1 for the sample (unbiased) estimator.
  • pandas defaults to ddof=1, unlike NumPy's ddof=0 default, a common source of mismatched results.
  • Standard aggregations propagate NaN; use nan-prefixed variants (nanmean, nansum, etc.) to ignore missing values.
  • argmin/argmax return the index of the extreme value, not the value itself.
  • np.percentile and np.median provide robust central-tendency measures less sensitive to outliers than the mean.

Practice what you learned

Was this page helpful?

Topics covered

#Python#PandasNumPyStudyNotes#DataScience#AggregationsAndStatistics#Aggregations#Statistics#Variance#Standard#StudyNotes#SkillVeris