Python HDFStore更新存储的HDF5数据帧
我有两个数据帧,Python HDFStore更新存储的HDF5数据帧,python,pandas,hdf5,Python,Pandas,Hdf5,我有两个数据帧,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附加到存储的df1,而不
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
附加到存储的df1
,而不是使用新的数据帧完全覆盖HDFStore
对象。这可能吗
此外,当我运行以下代码时,我返回ValueError只能附加到表中
。。。为什么会这样
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)
Per(我的重点):
HDFStore支持磁盘上的另一种PyTables格式,即表
格式。从概念上讲,表的形状非常类似于数据帧,包含行和行
列可以在相同或其他会话中追加表格。此外
支持删除和查询类型操作。此格式由指定
format='table'或format='t'追加或放入或到\u hdf
版本0.13中的新功能
此格式可以设置为一个选项以及pd.set_选项('io.hdf.default_format','table'),以启用put/append/to_hdf to默认以表格格式存储
[361]中的:store=pd.HDFStore('store.h5'))
In[362]:df1=df[0:4]
在[363]中:df2=df[4:]
#追加数据(自动创建表)
[364]中的store.append('df',df1)
[365]中的store.append('df',df2)
在[366]中:存储
出[366]:
文件路径:store.h5
#选择整个对象
在[367]中:存储。选择('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
#存储数据的类型
在[368]中:store.root.df.\u v\u attrs.pandas\u type
Out[368]:“frame_table”
注意:您还可以通过将format='table'或format='t'传递给put操作来创建表
我正在查看文档,并且能够找到它,但是如果您能详细说明为什么
store.append('df',df2)
返回ValueError:只能追加到表中
??您的hdf5
文件必须在中创建(与相反)。使用df.to_hdf(文件名,'df',mode='w',format='table')
创建它,或者设置pd.set_选项('io.hdf.default_format','table')
,以便format='table'
是默认格式。
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'