How can a One-Way ANOVA be performed in Python?

A One-Way ANOVA (Analysis of Variance) is a statistical test used to compare the means of three or more groups. This test is commonly used in scientific research and data analysis to determine if there is a significant difference between the means of multiple groups. In Python, a One-Way ANOVA can be performed using the built-in function “scipy.stats.f_oneway()” from the SciPy library. This function takes in the data from each group as input and returns the F-statistic and p-value, which can be used to determine the significance of the results. Additionally, there are several Python packages, such as statsmodels and pingouin, that provide more advanced options and visualizations for performing One-Way ANOVA. Overall, performing a One-Way ANOVA in Python is a straightforward and efficient process that allows for easy analysis of data with multiple groups.

Perform a One-Way ANOVA in Python


A (“analysis of variance”) is used to determine whether or not there is a statistically significant difference between the means of three or more independent groups.

This tutorial explains how to perform a one-way ANOVA in Python.

Example: One-Way ANOVA in Python

A researcher recruits 30 students to participate in a study. The students are to use one of three studying techniques for the next three weeks to prepare for an exam. At the end of the three weeks, all of the students take the same test.

Use the following steps to perform a one-way ANOVA to determine if the average scores are the same across all three groups.

Step 1: Enter the data.

First, we’ll enter the exam scores for each group into three separate arrays:

#enter exam scores for each group
group1 = [85, 86, 88, 75, 78, 94, 98, 79, 71, 80]
group2 = [91, 92, 93, 85, 87, 84, 82, 88, 95, 96]
group3 = [79, 78, 88, 94, 92, 85, 83, 85, 82, 81]

Step 2: Perform the one-way ANOVA.

Next, we’ll use the  from the SciPy library to perform the one-way ANOVA:

from scipy.stats import f_oneway

#perform one-way ANOVA
f_oneway(group1, group2, group3)

(statistic=2.3575, pvalue=0.1138)

Step 3: Interpret the results.

A one-way ANOVA uses the following null and alternative :

  • H(null hypothesis): μ1 = μ2 = μ= … = μ(all the population means are equal)
  • H(null hypothesis): at least one population mean is different from the rest

The is 2.3575 and the corresponding p-value is 0.1138. Since the p-value is not less than .05, we fail to reject the null hypothesis.

This means we do not have sufficient evidence to say that there is a difference in exam scores among the three studying techniques.

Additional Resources

x