I'm not sure to fully understand what you expect. Isn't df.info()
sufficient to help users?
>>> df.info()
<class '__main__.MyDataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 4 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 a 3 non-null int64
1 b 3 non-null float64
2 c 3 non-null object
3 d 3 non-null float64
dtypes: float64(2), int64(1), object(1)
memory usage: 224.0+ bytes
If not, you can subclassing DataFrame
and override methods like info
and __repr__
. You can store additional informations in attrs
dictionary and use it in these methods. Here an example:
class MyDataFrame(pd.DataFrame):
def info(self):
super().info()
s = '\nMore information as footer:\n'
s += self.attrs.get('more_info')
print(s)
def __repr__(self):
s = 'More information as header:\n'
s += f"{self.attrs.get('more_info')}\n\n"
s += super().__repr__()
return s
@property
def _constructor(self):
return MyDataFrame
def generate_data(bunch, of, inputs) -> pd.DataFrame:
df = MyDataFrame({'a': [0, 1, 2], 'b': [1.0, 1.1, 1.2],
'c': ['A', 'B', 'C'], 'd': [0.99, 2.49, 3.99]})
df.attrs = {
'more_info': 'Additional information here'
}
return df
df = generate_data('nothing', 'to', 'do')
Usage:
>>> df
More information as header: # <- HERE
Additional information here # <- HERE
a b c d
0 0 1.0 A 0.99
1 1 1.1 B 2.49
2 2 1.2 C 3.99
>>> df.info()
<class '__main__.MyDataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 4 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 a 3 non-null int64
1 b 3 non-null float64
2 c 3 non-null object
3 d 3 non-null float64
dtypes: float64(2), int64(1), object(1)
memory usage: 224.0+ bytes
More information as footer: # <- HERE
Additional information here # <- HERE
>>> df[['a', 'b']]
More information as header:
Additional information here
a b
0 0 1.0
1 1 1.1
2 2 1.2
I just used a simple string but you can have a more complex attrs
structure and a special function to display this dict (check if columns exist and avoid display useless information). I hope this helps.