Adding a description to a Pandas DataFrame column as an attribute

Viewed 325

I would like to a add a description of each column in my DataFrame as a per-column attribute. To recap what's on SO, there was a similar question asked about 8 years ago at Adding my own description attribute to a Pandas DataFrame with no answer. More recently, this question was asked again at Python Dataframe Adding a description to column and received an answer that uses _metadata and .attrs. However, the answer seems to be geared to dataframe-level attributes, not column-level ones. Setting column-level attributes with that approach does not propagate those properties after manipulation. For instance:

import pandas as pd

class SubclassedDataFrame(pd.DataFrame):

    # normal properties
    _metadata = ['description']

    @property
    def _constructor(self):
        return SubclassedDataFrame

data = {"a": [1, 2, 3], "b": [10, 12, 13]}

df = SubclassedDataFrame(data)

df.description = "About my data"
df['a'].description = 'column a description'

Results:

df.description   # prints 'About my data'
df.head().description   # prints 'About my data' after manipulation
df['a'].description   # prints 'column a description'
df['a'].head().description   # **raises AttributeError**

How can I set column-level attributes that persist after manipulation? I apologise if I have missed something obvious.

0 Answers
Related