将元信息/元数据添加到 Pandas DataFrame [英] Adding meta-information/metadata to pandas DataFrame
问题描述
是否可以向 Pandas DataFrame 添加一些元信息/元数据?
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.
一种解决方法是创建一个包含该信息的列,但在每一行中存储一条信息似乎很浪费!
推荐答案
当然,像大多数 Python 对象一样,您可以将新属性附加到 pandas.DataFrame
:
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'
但是请注意,虽然您可以将属性附加到 DataFrame,但在 DataFrame 上执行的操作(例如 groupby
、pivot
、join
> 或 loc
仅举几例)可能会返回一个新的 DataFrame, 没有附加元数据.Pandas 还没有一种强大的传播方法附加到 DataFrames 的元数据一>.
Note, however, that while you can attach attributes to a DataFrame, operations performed on the DataFrame (such as groupby
, pivot
, join
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.
将元数据保存在文件中是可能的.您可以在此处找到有关如何在 HDF5 文件中存储元数据的示例.
Preserving the metadata in a file is possible. You can find an example of how to store metadata in an HDF5 file here.
这篇关于将元信息/元数据添加到 Pandas DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!