我正在从图像文件中读取数据,并且想将此数据附加到单个 HDF 文件中。这是我的代码:
datafile = pd.HDFStore(os.path.join(path,'imageData.h5'))
for file in fileList:
data = {'X Position' : pd.Series(xpos, index=index1),
'Y Position' : pd.Series(ypos, index=index1),
'Major Axis Length' : pd.Series(major, index=index1),
'Minor Axis Length' : pd.Series(minor, index=index1),
'X Velocity' : pd.Series(xVelocity, index=index1),
'Y Velocity' : pd.Series(yVelocity, index=index1) }
df = pd.DataFrame(data)
datafile['df'] = df
datafile.close()
这显然是不正确的,因为每次循环运行时它都会用新数据覆盖每组数据。
如果我使用 datafile['df'] = df
而不是
datafile.append('df',df)
或者
df.to_hdf(os.path.join(path,'imageData.h5'), 'df', append=True, format = 'table')
我收到错误:
ValueError: Can only append to Tables
我已经提到了documentation及其他SO questions ,无济于事。
所以,我希望有人能够解释为什么这不起作用以及我如何成功地将所有数据附加到一个文件中。如果有必要,我愿意使用不同的方法(也许是 pyTables)。
任何帮助将不胜感激。
最佳答案
这将在 0.11 中起作用。创建组后(例如存储数据的标签,此处为“df”)。如果您存储固定格式,它将被覆盖(如果您尝试追加,则会出现上述错误消息);如果您编写table
格式,您可以附加。请注意,在 0.11 中,to_hdf
无法正确地将关键字传递给底层函数,因此您只能使用它来编写固定
格式。
datafile = pd.HDFStore(os.path.join(path,'imageData.h5'),mode='w')
for file in fileList:
data = {'X Position' : pd.Series(xpos, index=index1),
'Y Position' : pd.Series(ypos, index=index1),
'Major Axis Length' : pd.Series(major, index=index1),
'Minor Axis Length' : pd.Series(minor, index=index1),
'X Velocity' : pd.Series(xVelocity, index=index1),
'Y Velocity' : pd.Series(yVelocity, index=index1) }
df = pd.DataFrame(data)
datafile.append('df',df)
datafile.close
关于Python 2.7 : Appending Data to Table in Pandas,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22035360/