VOOZH about

URL: https://www.analyticsvidhya.com/blog/2024/01/various-uses-of-python-statistics-module-its-functions/

โ‡ฑ Various Uses of Python Statistics Module & Its Functions - Analytics Vidhya


India's Most Futuristic AI Conference Is Back โ€“ Bigger, Sharper, Bolder

  • d
  • :
  • h
  • :
  • m
  • :
  • s

Various Uses of Python Statistics Module & Its Functions

Sakshi Raheja Last Updated : 26 May, 2025
3 min read

Introduction

Python is a powerful programming language that offers a wide range of modules for various applications. One such module is the statistics module, which provides a comprehensive set of functions for statistical operations. In this blog, we will explore the Python statistics module in detail, covering all the methods, how to use them, and where to use them.

๐Ÿ‘ Python Statistics module

Python has rapidly become the go-to language in data science and is among the first things recruiters search for in a data scientistโ€™s skill set. Are you looking to learn Python to switch to a data science career?

Mathematical Statistics Functions

The Python statistics module is a powerful tool for performing mathematical statistics functions. It provides a wide range of functions for calculating measures of central tendency, dispersion, and more. For example, the mean, median, mode, variance, and standard deviation can all be easily calculated using the statistics module.

Functions: Calculate Measures of Central Tendency

  • mean(data): Calculates the arithmetic mean (average).
  • median(data): Calculates the median (middle value).
  • median_low(data): Calculates the low median of a multiset.
  • median_high(data): Calculates the high median of a multiset.
  • median_grouped(data, interval=1): Calculates the median of grouped continuous data.
  • mode(data): Calculates the most frequent value(s) (mode).

Functions: Measures of Dispersion

  • pstdev(data, mu=None): Calculates the population standard deviation.
  • pvariance(data, mu=None): Calculates the population variance.
  • stdev(data, xbar=None): Calculates the sample standard deviation.
  • variance(data, xbar=None): Calculates the sample variance.

Example:

import statistics

data = [1, 4, 6, 2, 3, 5]

mean = statistics.mean(data)

median = statistics.median(data)

stdev = statistics.stdev(data)

print("Mean:", mean)

print("Median:", median)

print("Standard deviation:", stdev)

Output:

Mean: 3.5

Median: 3.5

Standard deviation: 1.8708286933869707

Describing Your Data

In addition to basic statistical functions, the Python statistics module also allows you to describe your data in detail. This includes calculating the range, quartiles, and other descriptive statistics. These functions are extremely useful for gaining insights into the distribution and characteristics of your data.

Functions Describing your Data

  • quantiles(data, n=4): Divides data into equal-sized groups (quartiles by default).
  • fmean(data): Handles finite iterables gracefully.
  • harmonic_mean(data): Useful for rates and ratios.
  • geometric_mean(data): For values representing growth rates.
  • multimode(data): Returns all modes (not just one).

Example:

import statistics

data = [1, 4, 6, 2, 3, 4, 4]  # Example dataset

quartiles = statistics.quantiles(data)

fmean = statistics.fmean(data)

print("Quartiles:", quartiles)

print("FMean:", fmean)

Output:

Quartiles: [2.0, 4.0, 4.0]

FMean: 3.4285714285714284

Dealing with Missing Data

One common challenge in data analysis is dealing with missing values. The Python statistics module provides functions for handling missing data, such as removing or imputing missing values. This is essential for ensuring the accuracy and reliability of your statistical analysis.

Example: Imputing Missing Value with mean

import statistics

data = [1, 4, None, 6, 2, 3]

mean = statistics.mean(x for x in data if x is not None)

filled_data = [mean if x is None else x for x in data]

print(filled_data)

Output:

[1, 4, 3.2, 6, 2, 3]

Data Analysis Techniques

The Python statistics module is an integral part of various data analysis techniques. Whether youโ€™re performing hypothesis testing, regression analysis, or any other statistical analysis, the statistics module provides the necessary functions for carrying out these techniques. Understanding how to leverage the statistics module for different data analysis techniques is crucial for mastering Python statistics. Hereโ€™s an example of using the statistics module for hypothesis testing:

Example:

import statistics

import random

# Sample data

data = [1, 4, 6, 2, 3, 5]

# Calculate sample mean and standard deviation

sample_mean = statistics.mean(data)

sample_stdev = statistics.stdev(data)

# Generate many random samples with the same size as the original data

num_samples = 10000

random_means = []

for _ in range(num_samples):

   random_sample = random.choices(data, k=len(data))

   random_means.append(statistics.mean(random_sample))

# Calculate t-statistic

t_statistic = (sample_mean - 0) / (sample_stdev / (len(data) ** 0.5))  # Assuming a null hypothesis of 0

# Estimate p-value (proportion of random means more extreme than the sample mean)

p_value = (sum(1 for mean in random_means if abs(mean) >= abs(sample_mean))) / num_samples

print("t-statistic:", t_statistic)

print("p-value:", p_value)

Output:

t-statistic: 4.58257569495584

p-value: 0.5368

Conclusion

In conclusion, the Python statistics module is a versatile and powerful tool for performing statistical operations. Whether youโ€™re a data scientist, analyst, or researcher, mastering the statistics module is essential for gaining insights from your data. By understanding the various methods, how to use them, and where to use them, you can elevate your statistical analysis capabilities to new heights. So, start exploring the Python statistics module today and unlock its full potential for your data analysis needs.

Python has rapidly become the go-to language in data science and is among the first things recruiters search for in a data scientistโ€™s skill set. Are you looking to learn Python to switch to a data science career?

Also read:

Various Uses of Python Statistics Module & Its Functions

Introductory Statistics for Data Science!

I am a passionate writer and avid reader who finds joy in weaving stories through the lens of data analytics and visualization. With a knack for blending creativity with numbers, I transform complex datasets into compelling narratives. Whether it's writing insightful blogs or crafting visual stories from data, I navigate both worlds with ease and enthusiasm. 

A lover of both chai and coffee, I believe the right brew sparks creativity and sharpens focusโ€”fueling my journey in the ever-evolving field of analytics. For me, every dataset holds a story, and I am always on a quest to uncover it.

Login to continue reading and enjoy expert-curated content.

Free Courses

Generative AI - A Way of Life

Explore Generative AI for beginners: create text and images, use top AI tools, learn practical skills, and ethics.

Getting Started with Large Language Models

Master Large Language Models (LLMs) with this course, offering clear guidance in NLP and model training made simple.

Building LLM Applications using Prompt Engineering

This free course guides you on building LLM apps, mastering prompt engineering, and developing chatbots with enterprise data.

Improving Real World RAG Systems: Key Challenges & Practical Solutions

Explore practical solutions, advanced retrieval strategies, and agentic RAG systems to improve context, relevance, and accuracy in AI-driven applications.

Microsoft Excel: Formulas & Functions

Master MS Excel for data analysis with key formulas, functions, and LookUp tools in this comprehensive course.

Responses From Readers

Flagship Programs

GenAI Pinnacle Program| GenAI Pinnacle Plus Program| AI/ML BlackBelt Program| Agentic AI Pioneer Program

Free Courses

Generative AI| DeepSeek| OpenAI Agent SDK| LLM Applications using Prompt Engineering| DeepSeek from Scratch| Stability.AI| SSM & MAMBA| RAG Systems using LlamaIndex| Building LLMs for Code| Python| Microsoft Excel| Machine Learning| Deep Learning| Mastering Multimodal RAG| Introduction to Transformer Model| Bagging & Boosting| Loan Prediction| Time Series Forecasting| Tableau| Business Analytics| Vibe Coding in Windsurf| Model Deployment using FastAPI| Building Data Analyst AI Agent| Getting started with OpenAI o3-mini| Introduction to Transformers and Attention Mechanisms

Popular Categories

AI Agents| Generative AI| Prompt Engineering| Generative AI Application| News| Technical Guides| AI Tools| Interview Preparation| Research Papers| Success Stories| Quiz| Use Cases| Listicles

Generative AI Tools and Techniques

GANs| VAEs| Transformers| StyleGAN| Pix2Pix| Autoencoders| GPT| BERT| Word2Vec| LSTM| Attention Mechanisms| Diffusion Models| LLMs| SLMs| Encoder Decoder Models| Prompt Engineering| LangChain| LlamaIndex| RAG| Fine-tuning| LangChain AI Agent| Multimodal Models| RNNs| DCGAN| ProGAN| Text-to-Image Models| DDPM| Document Question Answering| Imagen| T5 (Text-to-Text Transfer Transformer)| Seq2seq Models| WaveNet| Attention Is All You Need (Transformer Architecture) | WindSurf| Cursor

Popular GenAI Models

Llama 4| Llama 3.1| GPT 4.5| GPT 4.1| GPT 4o| o3-mini| Sora| DeepSeek R1| DeepSeek V3| Janus Pro| Veo 2| Gemini 2.5 Pro| Gemini 2.0| Gemma 3| Claude Sonnet 3.7| Claude 3.5 Sonnet| Phi 4| Phi 3.5| Mistral Small 3.1| Mistral NeMo| Mistral-7b| Bedrock| Vertex AI| Qwen QwQ 32B| Qwen 2| Qwen 2.5 VL| Qwen Chat| Grok 3

AI Development Frameworks

n8n| LangChain| Agent SDK| A2A by Google| SmolAgents| LangGraph| CrewAI| Agno| LangFlow| AutoGen| LlamaIndex| Swarm| AutoGPT

Data Science Tools and Techniques

Python| R| SQL| Jupyter Notebooks| TensorFlow| Scikit-learn| PyTorch| Tableau| Apache Spark| Matplotlib| Seaborn| Pandas| Hadoop| Docker| Git| Keras| Apache Kafka| AWS| NLP| Random Forest| Computer Vision| Data Visualization| Data Exploration| Big Data| Common Machine Learning Algorithms| Machine Learning| Google Data Science Agent
๐Ÿ‘ Av Logo White

Continue your learning for FREE

Forgot your password?
๐Ÿ‘ Av Logo White

Enter OTP sent to

Edit

Wrong OTP.

Enter the OTP

Resend OTP

Resend OTP in 45s

๐Ÿ‘ Popup Banner
๐Ÿ‘ AI Popup Banner