python - Hdf5 文件中未显示列

标签 python pandas hdf5 vaex

我有一个大型数据集(13 亿数据),我想使用 Vaex 对其进行可视化。由于 csv 中的数据集非常大(520 个单独文件中大约 130gb),我将它们合并到一个 hdf5 文件中,并使用 pandas dataframe.to_hdf 函数(格式:表,附加到每个 csv 文件)。如果我使用pandas.read_hdf函数加载一片数据,就没有问题。

    x   y   z
0   -8274.591528    36.053843   24.766887
1   -8273.229203    34.853409   21.883050
2   -8289.577896    15.326737   26.041516
3   -8279.589741    27.798428   26.222326
4   -8272.836821    37.035071   24.795912
...     ...     ...     ...
995     -8258.567634    3.581020    23.955874
996     -8270.526953    4.373765    24.381293
997     -8287.429578    1.674278    25.838418
998     -8250.624879    4.884777    21.815401
999     -8287.115655    1.100695    25.931318

1000 rows × 3 columns

这就是它的样子,我可以访问我想要的任何列,并且形状应该是(1000,3)。但是,当我尝试使用 vaex.open 函数加载 hdf5 文件时:

 #  table
0   '(0, [-8274.59152784, 36.05384262, 24.7668...
1   '(1, [-8273.22920299, 34.85340869, 21.8830...
2   '(2, [-8289.5778959 , 15.32673748, 26.0415...
3   '(3, [-8279.58974054, 27.79842822, 26.2223...
4   '(4, [-8272.83682085, 37.0350707 , 24.7959...
...     ...
1,322,286,736   '(2792371, [-6781.56835851, 2229.30828904, -6...
1,322,286,737   '(2792372, [-6781.71119626, 2228.78749838, -6...
1,322,286,738   '(2792373, [-6779.3251589 , 2227.46826613, -6...
1,322,286,739   '(2792374, [-6777.26078082, 2229.49535808, -6...
1,322,286,740   '(2792375, [-6782.81758335, 2228.87820639, -6...

这就是我得到的。形状为 (1322286741, 1),只有列是“table”。当我尝试将 vaex 导入的 hdf 调用为 galacto[0] 时:

[(0, [-8274.59152784,    36.05384262,    24.76688728])]

在 pandas 导入的数据中,第一行是 x、y、z 列。当我尝试检查另一个问题中的数据时,它也给出一个错误,说没有找到数据。所以我认为问题是 pandas 逐行附加 hdf5 文件,并且它在其他程序中不起作用。我有办法解决这个问题吗?

最佳答案

hdf5 与 JSON 和 xml 一样灵活,您可以按照您想要的任何方式存储数据。 Vaex 有自己的数据存储方式(您可以使用 h5ls utils 检查结构,非常简单),与 Pandas/PyTables 存储数据的方式不一致。

Vaex 将每一列存储为单个连续数组,如果您不处理所有列,这是最佳选择,并且可以轻松地将内存映射到(真正的)numpy 数组。 PyTables 将每一行(至少是相同类型的)彼此相邻存储。这意味着如果您计算 x 列的平均值,您就可以有效地检查所有数据。

由于 PyTables hdf5 的读取速度可能已经比 CSV 快得多,我建议您执行以下操作(未经测试,但应该能说明问题):

import vaex
import pandas as pd
import glob
# make sure dir vaex exists
for filename in glob.glob("pandas/*.hdf5"):  # assuming your files live there
    pdf = pd.read_hdf(filename)
    df = vaex.from_pandas(pdf)  # now df is a vaex dataframe
    df.export(filename.replace("pandas", "vaex"), progress=True)) # same in vaex' format

df = vaex.open("vaex/*.hdf5")  # it will be concatenated
# don't access df.x.values since it's not a 'real' numpy array, but 
# a lazily concatenated column, so it would need to memory copy.
# If you need that, you can optionally do (and for extra performance)
# df.export("big.hdf5", progress=True)
# df_single = vaex.open("big.hdf5")
# df_single.x.values  # this should reference the original data on disk (no mem copy)

关于python - Hdf5 文件中未显示列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59347221/

相关文章:

matlab - 使用 h5py 读取 matlab .mat 文件

python - Django CentOS 7 - 无法导入名称 Col

python - 有什么方法可以在 matplotlib 中一次分配多个 xlabels 吗?

python - 将一系列值随机插入到 pd.dataframe 中

python - 将 scipy 稀疏矩阵存储为 HDF5

python - 如何使用 NumPy ndarray 从 HDF5 数据集共享内存

python - 在类对象中,如何自动更新属性?

相当于 ruby​​ Gem 文件的 Python

python - "Group By"HDFStore 中大数据的多个列

python - "TypeError: Singleton array cannot be considered a valid collection"使用sklearn train_test_split