[Answer constructed from comments above as I came here looking for it myself.]
In the current version of pandas
there is no such way that I know of.
To achieve the same thing with a bit of bookkeeping, you could have a function to create the computed column
def update_computed_column(df):
df['c'] = df['a'] / df['b']
and then call it whenever you are interested in checking the value.
Alternatively, wrap the DataFrame
in a class with a getter.
class WrappedDataFrame:
def __init__(self, df):
self._df = df
self._update_computed_columns()
def _update_computed_columns(self):
# Define all your computed columns
self._df['c'] = self._df['a'] / self._df['b']
@property
def df(self):
self._update_computed_columns()
return self._df
So then modifying the data will automatically recompute the columns.
>>> a = WrappedDataFrame(DataFrame.from_dict({'a': [1, 2, 3], 'b': [4, 5, 6]}))
>>> print(a.df)
a b c
0 1 4 0.25
1 2 5 0.40
2 3 6 0.50
>>> a.df['a'] = [7, 8, 9]
>>> print(a.df)
a b c
0 7 4 1.75
1 8 5 1.60
2 9 6 1.50
This could be augmented with methods to add new computed columns, storing their formulae as functions in a private dictionary, etc.
Alternatively, you could subclass DataFrame
and integrate the computation directly --- depends on your purpose.