小编Ale*_*Vis的帖子

如何将熊猫数据框写入HDF5数据集

我正在尝试将Pandas数据框中的数据写入嵌套的hdf5文件中,每个组中包含多个组和数据集。我想将其作为单个文件保存,将来会每天增加。我使用了以下代码,该代码显示了我想要实现的结构

import h5py
import numpy as np
import pandas as pd

file = h5py.File('database.h5','w')

d = {'one' : pd.Series([1., 2., 3.], index=['a', 'b', 'c']),
     'two' : pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}

df = pd.DataFrame(d) 

groups = ['A','B','C']         

for m in groups:

    group = file.create_group(m)
    dataset = ['1','2','3']

    for n in dataset:

        data = df
        ds = group.create_dataset(m + n, data.shape)
        print ("Dataset dataspace is", ds.shape)
        print ("Dataset Numpy datatype is", ds.dtype)
        print ("Dataset name is", ds.name)
        print …
Run Code Online (Sandbox Code Playgroud)

hdf5 python-3.x h5py pandas

6
推荐指数
1
解决办法
9097
查看次数

标签 统计

h5py ×1

hdf5 ×1

pandas ×1

python-3.x ×1