【问题标题】:writing pandas dataframe to hdf5 using pytables module使用 pytables 模块将 pandas 数据帧写入 hdf5
【发布时间】:2016-12-10 04:42:34
【问题描述】:

在下面的示例中,我正在尝试将多索引 pandas.DataFrame 存储在 h5 文件格式中

import pandas as pd
import numpy as np
store = pd.HDFStore('c:/temp/example.h5')
idx = pd.MultiIndex.from_arrays([['first']*3,['one','two','three']], names = ['outer','inner'])
abc = pd.DataFrame(np.random.randint(1,100,9).reshape(-1,3), index = idx, columns = list('abc'))

如果我尝试存储上面的数据帧('abc')来存储,我得到一个错误,如下所示

store.put('abc',abc,format='t',append=True)
TypeError: shape must be an integer or sequence: 3L

我之前没有收到此错误,可能与 pytable 的版本更新有关。我目前使用的是 3.2.2 版。关于以 h5 格式存储数据帧的替代解决方案的任何指针?

【问题讨论】:

    标签: python pandas hdf5 pytables


    【解决方案1】:

    你有什么理由不能使用to_hdf function

    abc.to_hdf('c:/temp/example.h5')
    

    【讨论】:

    • 我仍然得到同样的错误,即使你提出了替代方案。我的猜测是,在底层,这两种方法都是一样的。
    猜你喜欢
    • 2020-01-24
    • 2013-12-03
    • 2020-04-03
    • 2012-04-03
    • 2019-01-05
    • 2018-04-20
    • 1970-01-01
    • 2023-03-25
    • 2021-03-02
    相关资源
    最近更新 更多