我只想从 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/