Being able to draw meaningful insights from data is an important skill. Statistical analysis is an important part of this process, which gives you tools to evaluate hypotheses and uncover trends in your data. Many statistical tests assume that your data are normally distributed, which may not always be the case. This is where the Mann-Whitney U test comes into play.

In this tutorial, **you’ll learn how to use the Mann-Whitney U test to compare the distributions of two independent groups that aren’t normally distributed**. By the end of this tutorial, you’ll have learned the following:

- What the Mann-Whitney U test is and why it matters
- How to conduct the Mann-Whitney U test in Python
- How to interpret the results of the Mann-Whitney U test in Python

Table of Contents

## Understanding the Mann-Whitney U Test

The Mann-Whitney U test, sometimes known as the Mann-Whitney-Wilcoxon test, **is a non-parametric statistical test that is used to determine whether there is a significant difference between the distributions of two independent groups.**

Ok, that might be a lot to take in, and that’s ok! Really, what the test measures is whether two groups that are not normally distributed (this is the non-parametric part) are different in a significant way.

If you’ve checked out some other tutorials, such as how to conduct a t-test in Python, you might be wondering why we need another test. The t-test assumes that your data are normally distributed. While this happens often, it doesn’t happen all the time (such as when dealing with income data).

### Assumptions of the Mann-Whitney U Test

One of the great aspects of the Mann-Whitney U test is that your data does not need to be normally distributed. However, it requires two important assumptions to be met:

- The data in each group are independent. This means that the observations in each group should be influenced by the observations the other group.
- The dependent variable should be at least ordinal, meaning that it can be ranked.

### Hypotheses in the Mann-Whitney U Test

Using statistical tests allows us to test different hypotheses. These are generally broken down into the **null hypothesis (H0)** and the **alternative hypothesis (H1)**. Let’s take a look at what these mean:

- The
**null hypothesis**assumes that there is no different in the distributions of the two groups, while - The
**alternative hypothesis**assumes that there is a difference in the distributions of the two groups.

Now that you have a good understanding of what the Mann-Whitney U Test is and when it’s used, let’s load and analyze a sample dataset.

## Conduct the Mann-Whitney U Test in Python

In order to conduct the Mann-Whitney U test, we may need to install SciPy. Because SciPy isn’t part of the standard Python library, we can use `pip`

to install it. In your terminal write the following command:

`pip install scipy`

Once the library is installed, we can load two sample datasets. Imagine that we want to assess the sales performance of two different marketing teams. We can load both of their sales into Python lists, as shown below:

```
# Sample data for sales generated by Strategy A
sales_strategy_a = [2000, 2200, 1800, 2500, 2300, 2100, 1900, 2400, 2600, 2300]
# Sample data for sales generated by Strategy B
sales_strategy_b = [1800, 1900, 1700, 2000, 2100, 1850, 1950, 2050, 2200, 2000]
```

Each dataset contains the sales figures for ten different instances, reflecting data over a period of time. We’ll use this data as part of our Mann-Whitney U test to see if there is a significant difference in the sales generated by the two strategies.

In order to conduct the test, we first import the function from SciPy, as shown below. We can then pass our two lists into the function:

```
# Conduct the Mann-Whitney U Test in Python
from scipy.stats import mannwhitneyu
statistic, p_value = mannwhitneyu(sales_strategy_a, sales_strategy_b)
print("Mann-Whitney U Test Statistic:", statistic)
print("P-value:", p_value)
# Returns:
# Mann-Whitney U Test Statistic: 79.0
# P-value: 0.030639455384984277
```

The function returns two values:

- The test statistic, and
- The p-value

Because the p-value is less than 0.05 (a common threshold), we can reject the null hypothesis and state that there is a significant difference between the two methods.

## Interpret the Mann-Whitney U Test in Python

Now that we have conducted the Mann-Whitney U test, let’s take a look at how we can interpret the results.

The **Test Statistic (U statistic)** represents the rank sum of one sample relative to the other. A higher test statistic indicates that one group tends to have higher values than the other. The rank sum refers to adding up the ranks of one dataset, relative to the other.

The **P-value** allows us to draw conclusions based on a significance level. In general, a significance level of 0.05 is used, indicating that there is a 5% chance of rejecting the null hypothesis when it’s actually true.

- If the p-value is less than alpha (in this case, 0.05), we reject the null hypothesis. This suggests that there is a difference between the distributions of the two groups that is statistically significant.
- If the p-value is greater than or equal to our alpha value, then we fail to reject the null hypothesis. This indicates that we do not have enough evidence to conclude a significant difference between the two distributions.

## Conclusion

The Mann-Whitney U test is a helpful tool when working with non-normally distributed data. In this tutorial, you learned how to understand the test and how to use it in Python.

We began by walking through some underlying assumptions of the test and when it’s a useful test to use. We then walked through a practical business example to illustrate how the test can be used and interpreted.

## Additional Resources

To learn more about related topics, check out the tutorials below: