问题描述
是否可以向熊猫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的元数据 a>.
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添加元信息/元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!