如何将meta_data添加到Pandas数据框? [英] How to add meta_data to Pandas dataframe?

查看:116
本文介绍了如何将meta_data添加到Pandas数据框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我大量使用Pandas数据框.并且需要在数据帧上附加一些数据,例如记录数据帧的诞生时间,数据帧的附加描述等.

I use Pandas dataframe heavily. And need to attach some data to the dataframe, for example to record the birth time of the dataframe, the additional description of the dataframe etc.

我只是找不到dataframe类的保留字段来保留数据.

I just can't find reserved fields of dataframe class to keep the data.

因此,我更改了core\frame.py文件以添加一行_reserved_slot = {}来解决我的问题.我在这里发布问题只是想知道这样做可以吗?还是有更好的方法将元数据附加到数据框/列/行等?

So I change the core\frame.py file to add a line _reserved_slot = {} to solve my issue. I post the question here is just want to know is it OK to do so ? Or is there better way to attach meta-data to dataframe/column/row etc?

#----------------------------------------------------------------------
# DataFrame class


class DataFrame(NDFrame):
    _auto_consolidate = True
    _verbose_info = True
    _het_axis = 1
    _col_klass = Series

    _AXIS_NUMBERS = {
        'index': 0,
        'columns': 1
    }

    _reserved_slot = {}  # Add by bigbug to keep extra data for dataframe

    _AXIS_NAMES = dict((v, k) for k, v in _AXIS_NUMBERS.iteritems()) 

(以witingkuo的方式添加演示消息)

>>> df = pd.DataFrame(np.random.randn(10,5), columns=list('ABCDEFGHIJKLMN')[0:5])
>>> df
        A       B       C       D       E
0  0.5890 -0.7683 -1.9752  0.7745  0.8019
1  1.1835  0.0873  0.3492  0.7749  1.1318
2  0.7476  0.4116  0.3427 -0.1355  1.8557
3  1.2738  0.7225 -0.8639 -0.7190 -0.2598
4 -0.3644 -0.4676  0.0837  0.1685  0.8199
5  0.4621 -0.2965  0.7061 -1.3920  0.6838
6 -0.4135 -0.4991  0.7277 -0.6099  1.8606
7 -1.0804 -0.3456  0.8979  0.3319 -1.1907
8 -0.3892  1.2319 -0.4735  0.8516  1.2431
9 -1.0527  0.9307  0.2740 -0.6909  0.4924
>>> df._test = 'hello'
>>> df2 = df.shift(1)
>>> print df2._test
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Python\lib\site-packages\pandas\core\frame.py", line 2051, in __getattr__
    (type(self).__name__, name))
AttributeError: 'DataFrame' object has no attribute '_test'
>>> 

推荐答案

当前不支持此功能.参见 https://github.com/pydata/pandas/issues/2485 .原因是这些属性的传播是不平凡的.您当然可以分配数据,但是几乎所有的熊猫操作都会返回一个新对象,分配的数据将在那里丢失.

This is not supported right now. See https://github.com/pydata/pandas/issues/2485. The reason is the propogation of these attributes is non-trivial. You can certainly assign data, but almost all pandas operations return a new object, where the assigned data will be lost.

这篇关于如何将meta_data添加到Pandas数据框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆