如何将大型多个数组分层写入h5文件?
假设我有 10000 个系统。对于每个系统,我有 2 个数据集:对于每个数据集,我有 x、y 和 y_err 数组。如何使用 h5py
或 pandas
将所有系统的数据放入 h5 文件中?下面给出详细描述。
Systems=np.arange(10000)
for sys in Systems:
x1,y1,y1_err=np.random.rand(100),np.random.rand(100),np.random.rand(100)
x2,y2,y2_err=np.random.rand(200),np.random.rand(200),np.random.rand(200)
我想以结构化方式将所有系统的 x1,y1,y1_err,x2,y2,y2_err 放入 h5 文件中。
抱歉,这可能是非常基本的任务,但我真的很挣扎。
Suppose I have 10000 systems. For each system I have 2 datasets: for each data set I have x,y and y_err arrays. How can I put the data for all the systems into a h5 file, either using h5py
or pandas
? Detailed description is given below.
Systems=np.arange(10000)
for sys in Systems:
x1,y1,y1_err=np.random.rand(100),np.random.rand(100),np.random.rand(100)
x2,y2,y2_err=np.random.rand(200),np.random.rand(200),np.random.rand(200)
I want to put x1,y1,y1_err,x2,y2,y2_err
for all the systems in to a h5 file in a structured manner.
Sorry, this might be very elementary task but I am really struggling.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建 HDF5 文件的另外两种方法是 h5py 和 PyTables 包。它们很相似,但都有独特的优势。我喜欢两者的一点:当您使用 HDFView 打开 HDF5 文件时,您可以在简单的表格布局(如电子表格)中查看数据。
我为每个都写了一个例子。只有 2 个函数不同:1) 使用
create_group()
创建组并使用 h5pycreate_dataset
与 PyTablescreate_table
创建数据集。两者都使用 numpy rearray 来命名数据列(又名 x1,y1,y1_err)。如果您不想命名列并且所有数据都是相同类型(例如,所有浮点数或所有整数),则该过程会稍微简单一些。以下是 h5py 的流程:
这是与 PyTables 相同的流程(包为
导入表
):Two other methods to create HDF5 files are the h5py and PyTables packages. They are similar but each has unique strengths. The thing I like about both: when you open the HDF5 file with HDFView, you can view the data in a simple table layout (like a spreadsheet).
I wrote an example for each. Only a 2 functions are different: 1) creating groups with
create_group()
and creating datasets with h5pycreate_dataset
vs PyTablescreate_table
. Both use a numpy recarray to name the data columns (akax1,y1,y1_err
). The process is slightly simpler if you don't want to name the columns and all the data is the same type (e.g., all floats or all ints).Here is the process for h5py:
Here is the same procedure with PyTables (package is
import tables
):我认为这应该有效:
I think this should work: