如何向pandas hdf5添加另一个数据帧

时间:2018-05-01 16:16:32

标签: python pandas hdf5

db = pd.HDFStore("database.h5")
df = pd.DataFrame(np.random.randn(8, 3), columns=['Col1', 'Col2', 'Col3'])
db.put("A1", df, format = 'table', data_columns = True)
db["A1"]

    Col1    Col2    Col3
0   1.036201    -0.395399   -0.741962
1   0.233349    -0.733992   0.754594

db.close()

现在我想添加另一个不相关的数据框(稍后在另一个文件中,所以我现在必须关闭数据库):

db = pd.read_hdf("database.h5")
db.put("A3", newdf, format='table', data_columns=True)

我收到错误'DataFrame' object has no attribute 'put',我发现db实际上是一个数据帧:

db
    Col1    Col2    Col3
0   1.036201    -0.395399   -0.741962
1   0.233349    -0.733992   0.75459

1 个答案:

答案 0 :(得分:1)

默认模式为a ppend,因此您只需以相同的方式打开文件,并根据需要添加更多dataFrame:

db = pd.HDFStore("database.h5", 'a')  # open existing store
db.put("A3", newdf, format='table', data_columns=True)
db.close()
相关问题