Reputation: 21
This may not be possible, or even a good idea but I wanted to see what options are available.
I want to create a subclass of a DataFrame that has a column as a property. E.g.
import pandas as pd
class DFWithProperties(pd.DataFrame):
"""A DataFrame that definitely has an 'a' column."""
@property
def depends_on_a(self):
return self["a"] + 1
df = DFWithProperties({"a": [1,2,3,4]})
I can obviously access this property in a similar way to other columns with
df.depends_on_a
But I also want to be able to access this column with something like
df["depends_on_a"]
or
df[["a", "depends_on_a"]]
Are there any neat tricks I can use here, or am I being too ambitious?
Upvotes: 0
Views: 548
Reputation: 1080
Yes! You can store whatever you would like as an attribute of the pandas DataFrame, simply by saving it. You can save it even as a column if need be.
For example:
df.depends_on_a = df["depends_on_a"]
(or whatever you need)
However, this is not related to the dataframe itself, so any changes made will have to be applied manually to both as it will not happen automatically.
Source: Adding meta-information/metadata to pandas DataFrame
Upvotes: 1