python - 从 h5 文件中读取 n 行

标签 python pandas

我只想从 h5 文件中读取 10 行:

df = pd.read_hdf('data.h5', 'cleanuserbase', start=0, stop=10)

但这不起作用,因为它会读取所有行。

最佳答案

这不适用于固定商店ATM(但适用于table商店,请参阅Andy的回答),请参阅未决问题here

也就是说,商店本身确实支持索引。它只是没有建成。这是一窥内部情况。

In [35]: df = DataFrame(np.random.randn(10,2),columns=list('ab'))

In [36]: store = pd.HDFStore('test.h5',mode='w')

In [37]: store.put('df',df)

In [38]: store
Out[38]: 
<class 'pandas.io.pytables.HDFStore'>
File path: test.h5
/df            frame        (shape->[10,2])

In [39]: mask = slice(4,10)

In [40]: s = store.get_storer('df').storable

In [41]: DataFrame(s.block0_values[mask],index=s.axis1[mask],columns=s.axis0)
Out[41]: 
axis0         a         b
4     -1.347325 -0.936605
5     -0.342814 -0.452055
6      0.951228  0.160918
7     -0.096133  0.816032
8     -0.731431  1.190047
9     -1.050826  0.348107

In [42]: store.close()

我认为这可能会引发 NotImplementedError 直到此问题得到解决。

关于python - 从 h5 文件中读取 n 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29069592/

相关文章:

python - 如何解析使用单引号的类似 JSON 的语法?

python - 通过函数传递变量/列表

没有图形移动(滚动)的 Python-matplotlib 实时绘图

python - 将组分配给 Pandas 列中的连续 1

python - 按 Ctrl+C 无法关闭套接字

python - 如何从 VLC 捕获异常?

python - 将列名称映射到随机森林特征重要性

python - 使用数据框分组显示折线图

python - 将数据更改为 float 后,数据透视表中没有可聚合的数字类型

python - 在条件匹配时使用 np.where 或类似方法创建数据框列