代码之家  ›  专栏  ›  技术社区  ›  Aran Freel

HDFStore更新存储的HDF5数据帧

  •  4
  • Aran Freel  · 技术社区  · 9 年前

    df1 pd.HDFStore 对象和要附加到数据帧的另一个。

    store = pd.HDFStore('dataframe_store.h5')
    
    df1 = pd.DataFrame(np.empty((100, 5)))
    df2 = pd.DataFrame(np.empty((100, 5)))
    
    store['df1'] = df1
    

    实际上,我希望最终结果等于。。。

    store['df1'] = df1.append(df2)
    

    我想附加 df2 ,而不是完全覆盖 HDFStore

    此外,当我运行以下代码时,我返回 ValueError can only append to Tables

    df = pd.DataFrame(np.empty((1000, 5)))
    df2 = pd.DataFrame(np.empty((1000, 5)))
    
    store = pd.HDFStore('store.h5')
    
    store['df'] = df
    
    store.append('df', df2)
    
    1 回复  |  直到 9 年前
        1
  •  1
  •   unutbu    9 年前

    the docs

    HDFStore支持磁盘上的另一种PyTables格式,即表 总体安排从概念上讲,表的形状非常像数据帧,包含行和 柱。 此外 删除(&D);支持查询类型操作。此格式由指定 format='table'或format='t'追加或放入或到hdf

    版本0.13中新增。

    此格式也可以设置为一个选项。set_option('io.hdf.default_format','table')启用put/append/to_hdf to默认以表格格式存储。

    In [361]: store = pd.HDFStore('store.h5')
    
    In [362]: df1 = df[0:4]
    
    In [363]: df2 = df[4:]
    
    # append data (creates a table automatically)
    In [364]: store.append('df', df1)
    
    In [365]: store.append('df', df2)
    
    In [366]: store
    Out[366]: 
    <class 'pandas.io.pytables.HDFStore'>
    File path: store.h5
    
    # select the entire object
    In [367]: store.select('df')
    Out[367]: 
                       A         B         C
    2000-01-01  0.887163  0.859588 -0.636524
    2000-01-02  0.015696 -2.242685  1.150036
    2000-01-03  0.991946  0.953324 -2.021255
    2000-01-04 -0.334077  0.002118  0.405453
    2000-01-05  0.289092  1.321158 -1.546906
    2000-01-06 -0.202646 -0.655969  0.193421
    2000-01-07  0.553439  1.318152 -0.469305
    2000-01-08  0.675554 -1.817027 -0.183109
    
    # the type of stored data
    In [368]: store.root.df._v_attrs.pandas_type
    Out[368]: 'frame_table'
    

    注意:您也可以通过将format='table'或format='t'传递给put操作来创建表。