0

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?

1 Answers1

1

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

Larry the Llama
  • 958
  • 3
  • 13
  • Thanks Larry, I think this is answering a slightly different question to the one I have. In my case I want to access the property with the [] notation, not just create a new column/attribute. Let me know if I've misunderstood your answer through. – Timothy Hyndman Nov 09 '21 at 04:24
  • @TimothyHyndman you are right: there is no way to access a column as an attribute (as far a my research has shown) because it is a dictionary, and any column names may conflict with inbuild functions such as .head, etc. Hope this helps – Larry the Llama Nov 09 '21 at 04:30