Skip to content
Advertisement

Removing min, max and calculating average

I have columns of numbers and I would need to remove only one min. and one max. and then calculate the average of the numbers that remain. The hitch is that the min/max could be anywhere in the column and some rows may be blank (null) or have a zero, or the column might have only 3 values. All numbers will be between 0 and 100. For example:

Value    Property
80          H
30.5        D
40          A
30.5        A
72          H
56          D
64.2        H

If there is more than one min or max, only one can be removed.

To calculate the minimum and maximum of a column, I did as follows:

maximum = df['Value'].max()
minimum = df['Value'].min()

In the condition for calculating this average, I also included the condition where it is not null and where it is not equal to zero. However, I do not know how to remove only one max and one min, and add information on greater than 3 rows/values.

I hope you can provide some help/tips on this.

Advertisement

Answer

If the objective is to calculate the average without one min and one max, you can just do

(df['Value'].sum() - df['Value'].min() - df['Value'].max())/(len(df)-2)

which outputs 52.54 for your data. Note that this will ignore NaNs etc. This will not modify your df which, if I read the question right, was not the objective anyway

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement