Python 2.7 : Appending Data to Table in Pandas

标签 python pandas hdf5 pytables

我正在从图像文件中读取数据,并且想将此数据附加到单个 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/

相关文章:

python - anaconda 中的 pygame 无法安装

python - 多索引数据框中多列标准的行选择(更有效的解决方案?)

python - 每个组中包含特定值的新数据框

python - 在不加载到内存的情况下将 HDF5 转换为 Parquet

c - 是否有预处理器选项来检查 hdf5 的可用性?

Python正则表达式判断一个字符是数字、字母数字还是在指定的字符串中

python - Pandas 在多索引 DataFrame 中使用 loc 进行赋值

python - 使用逻辑与运算符在 Pandas 中创建 DataFrame

Python:使用 HDF5 格式的不完整成对相异矩阵运行多维缩放

python - 为什么 Keras Tokenizer 文本到序列对所有文本返回相同的值?