Adding meta-information/metadata to pandas DataFrame
pandas, python
Solution
Sure, like most Python objects, you can attach new attributes to a `pandas.DataFrame`:
import pandas as pd
df = pd.DataFrame([])
df.instrument_name = 'Binky'
Note, however, that while you can attach attributes to a DataFrame, operations performed on the DataFrame (such as `groupby`, `pivot`, `join`, `assign` or `loc` to name just a few) may return a new DataFrame without the metadata attached. Pandas does not yet have a robust method of propagating metadata attached to DataFrames.
Preserving the metadata in a file is possible. You can find an example of how to store metadata in an HDF5 file here.
Problem
Is it possible to add some meta-information/metadata to a pandas DataFrame? For example, the instrument's name used to measure the data, the instrument responsible, etc. One workaround would be to create a column with that information, but it seems wasteful to store a single piece of information in every row!
Related problems
- Save additional attributes in Pandas Dataframe
- Saving and loading multiple objects in pickle file?
- Storing Pandas objects along with regular Python objects in HDF5
- Get the name of a pandas DataFrame
- How to write Parquet metadata with pyarrow?
- What is subclassing?
- Pandas _metadata of DataFrame persistence error