Member-only story
How to Extend Pandas DataFrames with Custom Methods to Supercharge Code Functionality and Readability
A step-by-step guide to extending pandas DataFrames with custom methods including full examples of how to implement extensions for conditional probability and expected values
Introduction
The Problem
Pandas data frames come with a wide range of in-built functions, but there are always new ways that Python programmers will want to interrogate and transform their data.
The Opportunity
A common approach is to write functions and pass the data frames as parameters but this can become unwieldly, whereas the optimum approach would be to add new methods and properties directly to the pandas classes.
The Way Forward
It is very easy to extend the functionality of classes in pandas or any other library with custom methods and properties producing code which is easy to read, understand and re-use.
Background
I had been writing some code for a causal inference library that involved some complex data operations and the constant…