# How to Calculate a Trimmed Mean in Python

### Introduction

In the realm of statistics, the trimmed mean is a robust measure of central tendency, often used when dealing with data that may contain outliers or non-normal distributions. Unlike the traditional mean, the trimmed mean takes a more refined approach, eliminating the influence of extreme values to provide a more accurate depiction of the data’s central tendency.

### What is a Trimmed Mean?

The trimmed mean, also known as a truncated mean or an adjusted mean, is a statistical measure that involves removing a certain percentage of the smallest and largest values before calculating the mean. The goal is to reduce the impact of outliers or extreme values that could potentially distort the overall interpretation of the data.

For example, when calculating the 10% trimmed mean of a dataset, one would remove the top 10% and bottom 10% of values, and then compute the average of the remaining data. By doing this, the trimmed mean offers a ‘middle ground’ between the mean (which uses all data points) and the median (which uses only the central data point or the average of the two central points).

### Why Use a Trimmed Mean?

The use of a trimmed mean comes in handy when a dataset contains outliers, skewed distributions, or is heavily tailed. These data characteristics can significantly affect the traditional mean, making it an inaccurate representation of the data’s central tendency.

By trimming away the extremes, this method offers a more robust estimate of the data’s central location, which is less sensitive to outliers or skewed distributions. For instance, in income data, where a few high incomes can drastically raise the mean, a trimmed mean can provide a more representative average income.

### How to Calculate a Trimmed Mean?

Here’s a step-by-step process for calculating a 10% trimmed mean:

1. Arrange the data: Start by arranging the dataset in ascending order.
2. Determine the trimming percentage: Decide on the percentage of values to be trimmed from each end of the dataset. In this case, 10%.
3. Calculate the number of values to trim: Multiply the total number of data points by the trimming percentage to find the number of values to be removed from each end. If this results in a decimal, round up or down as appropriate.
4. Trim the data: Remove the calculated number of values from each end of your arranged data.
5. Compute the mean: Calculate the average of the remaining values.

## How to Calculate a Trimmed Mean in Python?

Before we delve into the calculations, make sure you have the necessary Python libraries installed. If not, you can install them using pip, Python’s package manager.

pip install numpy scipy

Here, we’re installing NumPy for handling numerical data and SciPy for its statistical functions.

### Importing the Libraries

After installation, you need to import these libraries into your Python environment.

import numpy as np
from scipy import stats

### Calculating the Trimmed Mean

Let’s say we have the following list of numbers for which we want to calculate a 10% trimmed mean:

data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Here’s how to do it:

trimmed_mean = stats.trim_mean(data, 0.1)

The trim_mean function from the scipy.stats module calculates the trimmed mean. The first argument is the dataset, and the second argument is the proportion to cut from each end of the sorted data.

Note: The proportion should be between 0 and 0.5. If you want to remove the top and bottom 10%, the proportion will be 0.1.

Let’s print the result:

print("The 10% trimmed mean is: ", trimmed_mean)

### A More Detailed Example

Let’s consider a more complex dataset with outliers:

data = [1, 2, 3, 4, 5, 6, 20, 22, 99, 100]

We can calculate the mean, median, and 20% trimmed mean as follows:

mean = np.mean(data)
median = np.median(data)
trimmed_mean = stats.trim_mean(data, 0.2)

print("Mean: ", mean)
print("Median: ", median)
print("20% Trimmed mean: ", trimmed_mean)

### Calculate the Trimmed Mean of a column in Pandas

you can also calculate the trimmed mean of a column in a pandas DataFrame as well. Here’s how to do it:

First, let’s make sure you have Pandas and SciPy installed. If not, install them using pip:

pip install pandas scipy

Then, import the necessary libraries:

import pandas as pd
from scipy import stats

Let’s say we have a pandas DataFrame with a column named ‘values’:

df = pd.DataFrame({
'values': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 25, 99, 100]
})

We can calculate the trimmed mean for the ‘values’ column as follows:

trimmed_mean = stats.trim_mean(df['values'], 0.1)

Here, we are calculating the 10% trimmed mean of the ‘values’ column. The trim_mean function takes the column as the first argument and the proportion to cut off from each end as the second argument.

Print the result:

print("The 10% trimmed mean is: ", trimmed_mean)

If you want to apply the trimmed mean to multiple columns, you can use a loop or a function to iterate over the columns in your DataFrame.

Remember to adjust the proportion of trimming based on your specific data and the presence of outliers. Trimming too much can result in a loss of data, while trimming too little may not sufficiently reduce the impact of outliers.

### Conclusion

The trimmed mean is a valuable measure of central tendency, particularly when dealing with datasets that include outliers. By trimming the extreme ends of the data, we can obtain an average that is more representative of the central tendency of the overall dataset.

Python, with its rich ecosystem of data science libraries, makes it simple and straightforward to calculate the trimmed mean. The scipy.stats module’s trim_mean function is a particularly useful tool for this purpose.

Remember to consider the characteristics of your data when deciding on the appropriate trimming proportion. While the trimmed mean can help address the presence of outliers, over-trimming can potentially result in the loss of valuable data.