Pandas includes multiple built in functions such as sum
, mean
,
max
, min
, etc. that you can apply to a DataFrame or grouped data.
However, building and using your own function is a good way to learn more about
how pandas works and can increase your productivity with data wrangling and analysis.
The weighted average is a good example use case because it is easy to understand but useful formula
that is not included in pandas. I find that it can be more intuitive than a simple average
when looking at certain collections of data. Building a weighted average function
in pandas is relatively simple but can be incredibly useful when combined with
other pandas functions such as groupby
.
This article will discuss the basics of why you might choose to use a weighted
average to look at your data then walk through how to build and use this function
in pandas. The basic principles shown in this article will be helpful for building
more complex analysis in pandas and should also be helpful in understanding
how to work with grouped data in pandas.
Read more...