How to Compute Key Statistics with NumPy and Pandas DataFrames
This guide shows how to calculate common statistical measures such as mean, median, range, variance, standard deviation, covariance, and correlation using NumPy functions, and demonstrates the equivalent operations with Pandas DataFrames, including a table of useful DataFrame methods for statistical analysis.
Using NumPy to Compute Statistics
The NumPy library provides functions for calculating a variety of statistical metrics. You can also use functions from scipy.stats for the same purpose. The table below lists common NumPy statistical functions.
Example: A school randomly selects 100 students and records their height and weight. Using the data, compute the height's mean, median, range, variance, and standard deviation, as well as the covariance and correlation between height and weight.
The computed covariance and correlation values are shown in the original source. The resulting statistics for height are: mean, median, range, variance, and standard deviation (values omitted here). The covariance and correlation between height and weight are also provided.
Using Pandas DataFrame to Compute Statistics
Pandas DataFrames offer a collection of built‑in statistical methods. The following list summarizes several useful functions.
count : Returns the number of non‑NaN entries.
mad : Computes the median absolute deviation.
mode : Returns the mode(s), i.e., the most frequently occurring value(s).
skew : Returns the skewness of the data.
kurt : Returns the kurtosis of the data.
quantile : Returns sample quantiles; by default it returns the 50th percentile (median).
Model Perspective
Insights, knowledge, and enjoyment from a mathematical modeling researcher and educator. Hosted by Haihua Wang, a modeling instructor and author of "Clever Use of Chat for Mathematical Modeling", "Modeling: The Mathematics of Thinking", "Mathematical Modeling Practice: A Hands‑On Guide to Competitions", and co‑author of "Mathematical Modeling: Teaching Design and Cases".
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.