Timothy Hyndman
Timothy Hyndman

Reputation: 21

How to create a property of a subclassed pandas dataframe that acts like a column

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

Answers (1)

Larry the Llama
Larry the Llama

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

Related Questions