python - Pandas HDFStore 用于核外顺序读/写可变大小的集合

标签 python pandas large-files

我想增量地读取和写入 hdf5 文件中的数据,因为我无法将数据装入内存。

要读/写的数据是整数集。我只需要按顺序读/写这些集合。无需随机访问。就像我读 set1,然后 set2,然后 set3,等等。

问题是我无法通过索引检索集合。

import pandas as pd    
x = pd.HDFStore('test.hf', 'w', append=True)
a = pd.Series([1])
x.append('dframe', a, index=True)
b = pd.Series([10,2])
x.append('dframe', b, index=True)
x.close()

x = pd.HDFStore('test.hf', 'r')
print(x['dframe'])
y=x.select('dframe',start=0,stop=1)
print("selected:", y)
x.close()

输出:

0     1
0    10
1     2
dtype: int64
selected: 0    1
dtype: int64

它没有选择我的第 0 组,即 {1,10}

最佳答案

这个方法有效。但我真的不知道这有多快。

这会扫描整个文件以查找具有索引的行吗?

这会浪费时间。

import pandas as pd

x = pd.HDFStore('test.hf', 'w', append=True, format="table", complevel=9)
a = pd.Series([1])
x.append('dframe', a, index=True)
b = pd.Series([10,2])
x.append('dframe', b, index=True)
x.close()

x = pd.HDFStore('test.hf', 'r')
print(x['dframe'])
y=x.select('dframe','index == 0')
print('selected:')
for i in y:
    print(i)
x.close()

输出:

0     1
0    10
1     2
dtype: int64
selected:
1
10

关于python - Pandas HDFStore 用于核外顺序读/写可变大小的集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43017015/

相关文章:

python - Python 中的 Hot Deck 插补

python - AND 感知器的权重和偏差是多少?

python - 使用python解析大(9GB)文件

python - 在python中一切都是对象是什么意思

python - 在 C++ 中索引 tensorflow 输出张量

python - Django - 存储过程不存在

python - 从 Pandas 数据框创建二维数组

c# - 在 C# 中使用流读取大文本文件

带有大文件的 Git

python - 使用Python递归进行日常编码挑战