【问题标题】:Columns not recognized when importing HDF5 file导入 HDF5 文件时无法识别列
【发布时间】:2022-10-17 16:08:47
【问题描述】:

我正在尝试在 python 中导入HDF5 文件。我没有详细说明文件是如何编写的。因此,我尝试了vaexpandas 来打开它。如何指定我的列,以便它们被识别?

我试图检查文件的结构:

$ h5ls -v file.hdf5/DataSet
Opened "file.hdf5" with sec2 driver.
DataSet                  Dataset {5026/Inf}
    Attribute: Species scalar
        Type:      12-byte null-terminated ASCII string
    Attribute: Tuning scalar
        Type:      8-byte null-terminated ASCII string
    Location:  1:800
    Links:     1
    Chunks:    {1} 88 bytes
    Storage:   442288 logical bytes, 442288 allocated bytes, 100.00% utilization
    Type:      struct {
                   "Scan"             +0    native double
                   "col6"            +8    native double
                   "col5"            +16   native double
                   "col10"           +24   native double
                   "col7"            +32   native double
                   "col8"            +40   native double
                   "col1"            +48   native double
                   "col2"            +56   native double
                   "col4"            +64   native double
                   "col9"            +72   native double
                   "col3"            +80   native double
               } 88 bytes

vaex

当我使用vaex 时,无法识别各个列,所有数据都以单个列DataSet 结束。

import vaex as vx
df = vx.open('file.hdf5')
df
df['DataSet']

输出如下所示:

#      DataSet
0      '(0., 1.36110629e-11, 5.45816316e-09, 3.79845801...
1      '(1., 1.3613447e-11, 5.45889204e-09, 3.79879826e...
...
Expression = DataSet
Length: 5,026 dtype: [('Scan', '<f8'), ('col6', '<f8'), ('col5', '<f8'), ('col10', '<f8'), ('col7', '<f8'), ('col8', '<f8'), ('col1', '<f8'), ('col2', '<f8'), ('col4', '<f8'), ('col9', '<f8'), ('col3', '<f8')] (column)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
   0  '(0., 1.36110629e-11, 5.45816316e-09, 3.79845801...
   1  '(1., 1.3613447e-11, 5.45889204e-09, 3.79879826e...
...

有没有办法告诉vx.open 我的专栏是如何组织的?

熊猫

我尝试按照建议here 使用pandas 导入文件,但是

pd.read_hdf('file.hdf5')

结果为ValueError

【问题讨论】:

    标签: python pandas hdf5 vaex


    【解决方案1】:

    我使用h5py 包读取HDF5 文件并使用vaex.from_array 方法创建数据框。

    import vaex
    import h5py
    
    with h5py.File('file.hdf5', 'r') as data_file :
        dset = data_file['DataSet']
        df = vaex.from_arrays(Scan = dset['Scan'], Beam1 = dset['Beam1'], Beam2 = dset['Beam2'], Beam3 = dset['Beam3'], Beam4 = dset['Beam4'], Beam5 = dset['Beam5'], Beam6 = dset['Beam6'], Beam7 = dset['Beam7'], Beam8 = dset['Beam8'], Beam9 = dset['Beam9'], Beam10 = dset['Beam10'])
    df
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-25
      • 2020-12-17
      • 2020-07-24
      • 2023-03-31
      • 2021-04-23
      相关资源
      最近更新 更多