Python for Introductory StatisticsXYZ Homework Edition

⇩ Download ▾

Descriptive Stats

import numpy as np
import math
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,ddof=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,ddof=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! 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, 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.

These eBooks are a prerelease and are not yet certified conformant with WCAG 2.1 AA or any other accessibility standard. Every page is built against an automated accessibility gate, and our target for the published editions is WCAG 2.2 Level AA. If something is unusable, please tell us.