我现在有一个代码,它导入一个hdf5文件,然后计算曲线下一个区域的函数。
import h5py
file = h5py.File('/Users/hansari/Desktop/blabla', 'r')
xdata = file.get('data')
xdata= np.array(xdata)
xdata_df = pd.DataFrame(xdata)
table = pd.DataFrame(xdata_df).reset_index() 这是我用来获取文件的代码。
我目前有一个文件夹比有25个hdf5文件。是否有一种方法可以让代码运行所有的25个文件,并为所有人提供函数的结果?
我希望它导入文件,运行整个脚本,然后在下一个hdf5文件中重复它,而不是先导入所有数据,然后用大量的数据运行代码。
我目前正在使用glob.glob,但它正在一次性导入所有文件,并为我提供了一个很难处理的巨大数据集。
发布于 2022-03-31 18:32:54
如果没有更多的代码,我就不能告诉你你做错了什么。为了演示这个过程,我创建了一个简单的示例,它读取多个HDF5文件并使用glob.iglob()和h5py加载到Pandas数据框架中。请看下面的代码。table数据文件是在第二个循环中创建的,只包含来自1个HDF5文件的数据。您应该添加您的函数来计算for file in glob.iglob()循环中曲线下的面积。
# First, create 3 simple H5 files
for fcnt in range(1,4,1):
fname = f'file_{fcnt}.h5'
with h5py.File(fname,'w') as h5fw:
arr = np.random.random(10*10).reshape(10,10)
h5fw.create_dataset('data',data=arr)
# Loop over H5 files and load into a dataframe
for file in glob.iglob('file*.h5'):
with h5py.File(file, 'r') as h5fr:
xdata = h5fr['data'][()]
table = pd.DataFrame(xdata).reset_index()
print(table)
# add code to compute area under the curve herehttps://stackoverflow.com/questions/71679923
复制相似问题