5 Statistical Tests Made Simple with Python’s Pingouin Library

5 Statistical Tests Simple Python Pingouin Library
Image by Editor | ChatGPT
 

Among the many Python libraries for statistical analysis, one stands out for its delightfully simple syntax: Pingouin.

Compared with other Python statistical libraries such as SciPy or statsmodels, Pingouin emphasizes easily reproducible code, minimalistic syntax, consistent experiment modeling, and seamless use of pandas DataFrame objects to return publication-ready tabular outputs.

This article highlights that simplicity by illustrating five statistical tests that are easier to run with the Pingouin library. All five code examples are self-contained, each with the necessary imports so you can try them right away. You may need to pip install pingouin first if you’ve never used this library before.

The article assumes basic prior knowledge of statistical tests, their inputs, and how to interpret their results.

1. One-Sample t-Test

A one-sample t-test checks whether a sample is significantly different from a known (or hypothesized) population mean. It compares the sample mean to a specified constant value (the a priori hypothesized mean). Implementing this test with Pingouin is simple:

 
import pingouin as pg

data = [5.1, 4.8, 5.3, 5.0, 5.2]
pg.ttest(data, 5)

Highlight: We recommend that you try all the examples that follow to view results yourself. For this first one, you may be surprised at how neat yet detailed the output is. Each statistical test’s results (including the cornerstone p-value used to accept or reject hypotheses) are displayed in a clean table.

Results of an example statistical test with Pingouin

2. Independent Samples t-Test

With one line, we can perform an independent-samples t-test that checks for a statistically significant difference between the means of two unrelated groups, once again returning neat, detailed results in tabular form.

 
import pingouin as pg

pg.ttest([5.1, 5.3, 5.0], [4.7, 4.8, 4.9])

3. One-Way ANOVA

One-way ANOVA is used to compare means across several groups. This example applies it to four groups using the ‘anova’ example dataset available with the library. The between argument specifies the categorical factor used to define groups.

 
import pingouin as pg

df = pg.read_dataset('anova')
pg.anova(dv='Pain threshold', between='Hair color', data=df)

4. Repeated-Measures ANOVA

This ANOVA tests whether there is a significant difference in means across several conditions when the same participants are measured repeatedly (e.g., over time or under different conditions). Using a small example dataset, a single line of code is enough to run it.

 
import pandas as pd
import pingouin as pg

df = pd.DataFrame({
    'Subject': [1, 1, 1, 2, 2, 2, 3, 3, 3],
    'Time': ['T1', 'T2', 'T3'] * 3,
    'Score': [5, 6, 7, 4, 5, 6, 6, 7, 8]
})

pg.rm_anova(dv='Score', within='Time', subject='Subject', data=df)

5. Correlation Analyses

The last example shows how to apply different correlation approaches: standard Pearson correlation, rank-based Spearman correlation, and partial correlation (controlling for a third variable).

 
import pandas as pd
import pingouin as pg

x = [1, 2, 3, 4, 5]
y = [1.1, 1.9, 3.2, 3.8, 5.1]
z = [2.0, 2.2, 3.0, 3.9, 5.0]

df_corr = pd.DataFrame({'x': x, 'y': y, 'z': z})

pg.corr(df_corr['x'], df_corr['y'], method='pearson')
pg.corr(df_corr['x'], df_corr['y'], method='spearman')
pg.partial_corr(data=df_corr, x='x', y='y', covar='z')

If you are using a notebook, run the three correlation calls above in separate cells to view their individual outputs.

Wrapping Up

This article demonstrated how straightforward common statistical tests can be in Python using the Pingouin library. Five essential procedures — t-tests, ANOVAs, and correlation analysis — were illustrated with concise, reproducible code.

Leave a Reply

Your email address will not be published. Required fields are marked *