Login
📚 Python for Introductory Statistics: Lab Workbook
Chapters ▾

Descriptive Stats

import numpy as np
import pandas as pd

x=[x1,x2,x3,...]

x_df=pd.DataFrame({'column_name':x})
procedurenumpypandas
frequency counts np.unique(x,return_counts=True) x_df.value_counts
size np.size(x) x_df.count
min np.amin(x) x_df.min
max np.amax(x) x_df.max
mean np.mean(x) x_df.mean
modeNA x_df.mode
median np.median(x) x_df.median
sample variance np.var(x,ddf=1) x_df.var
pop variance np.var(x) x_df.var(ddof=0)
sample std np.std(x,ddof=1) x_df.std
pop std np.std(x) x_df.std(ddof=0)
weighted mean np.average(x,weights=freq) NA
weighted sample std (np.cov(x,fweights=freq,dd0f=1))**(1/2) NA
weighted pop std (np.cov(x,fweights=freq))**(1/2) NA
percentiles np.quantile(x,p) x_df.quantile(p)
summary statsNA x_df.describe
factorial, n! np.math.factorial(n) NA
n random integers from a to b np.random.randint(a,b+1,size=n) NA
weighted mean np.average(x,weights=y) NA
Weighted std (np.cov(x,aweights=y,ddof=0)**(1/2) NA
correlation coef. np.corrcoef(x,y) x_df.corr

NA = Not Available

Summary statistics by grouping with categorical columns:

x_df.groupby(['level','gender']).agg({'test1_score': ['mean','std','size']})

Adapted from Python for Introductory Statistics: Student's Lab Workbook, by Simon Aman (Truman College, City Colleges of Chicago), licensed under CC BY 4.0. Changes were made: reformatted as an accessible XYZ web edition with live in-browser code cells. License: CC-BY-4.0.