Today, I needed to calculate the range of a column in my dataframe alongside with other descriptive stats. Unfortunately, Pandas does not have a built in range function like min, max, std etc. Therefore, the only way to do this is to write the function and apply it to my dataframe.

def _range(series):
return series.max() — series.min()

The above function takes the series and returns the max-min of the series back. We can apply it as below:

print(data_agg[[‘bid_sessions’]].agg([‘min’, ‘max’,’mean’,’std’,_range]))

As you can see, we are using the aggregate function for the stats we are interested in and available in pandas, but we are also calling the _range function which we wrote.

This is a neat way of writing your own calculation that can be used within the aggregate function.

--

--

Bilal Mussa

Bilal Mussa

I enjoy finding solutions to business problems using data and data science. Keep it simple is my mantra.