Hdf5 文件中未显示的列
Columns not showing in Hdf5 file
我有一个大型数据集(13 亿数据),我想用 Vaex 进行可视化。由于 csv 中的数据集非常大(在 520 个单独的文件中大约 130gb),我将它们合并到一个 hdf5 文件中,其中包含 pandas dataframe.to_hdf 函数(格式:table,附加到每个 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)
我有一个大型数据集(13 亿数据),我想用 Vaex 进行可视化。由于 csv 中的数据集非常大(在 520 个单独的文件中大约 130gb),我将它们合并到一个 hdf5 文件中,其中包含 pandas dataframe.to_hdf 函数(格式:table,附加到每个 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)