[英]Python 2.7: Appending Data to Table in Pandas
I am reading data from image files and I want to append this data into a single HDF file. 我正在从图像文件中读取数据,并且希望将此数据附加到单个HDF文件中。 Here is my code:
这是我的代码:
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()
This is obviously incorrect as it overwrites each set of data with the new one each time the loop runs. 这显然是不正确的,因为每次循环运行时,都会用新数据覆盖新数据集。
If instead of datafile['df'] = df
, I use 如果使用
datafile['df'] = df
代替
datafile.append('df',df)
OR 要么
df.to_hdf(os.path.join(path,'imageData.h5'), 'df', append=True, format = 'table')
I get the error: 我得到错误:
ValueError: Can only append to Tables
I have referred to the documentation and other SO questions , without avail. 我已经参考了文档和其他SO问题 ,但无济于事。
So, I am hoping someone can explain why this isn't working and how I can successfully append all the data to one file. 因此,我希望有人能解释为什么它不起作用以及如何将所有数据成功附加到一个文件中。 I am willing to use a different method (perhaps pyTables) if necessary.
如果有必要,我愿意使用其他方法(也许是pyTables)。
Any help would be greatly appreciated. 任何帮助将不胜感激。
This will work in 0.11. 这将在0.11中起作用。 Once you create a group (eg the label where you are storing data, the 'df' here).
创建组后(例如,用于存储数据的标签,请在此处输入“ df”)。 If you store a
fixed
format it will overwrite (and if you try to append will give you the above error msg); 如果存储的是
fixed
格式,它将被覆盖(如果尝试追加,则会出现上述错误味精); if you write a table
format you can append. 如果编写
table
格式,则可以追加。 Note that in 0.11, to_hdf
does not correctly pass keywords thru to the underlying function so you can use it ONLY to write a fixed
format. 请注意,在0.11中,
to_hdf
不能正确地将关键字直通关键字传递给基础函数,因此您只能使用它编写fixed
格式。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.