python - 获取 pandas HDF5 查询中的最后一行

标签 python pandas hdf5

我正在尝试获取存储在 HDF5 中的 Pandas 数据帧最后一行的索引而不必将整个数据集或索引拉入内存。我正在寻找这样的东西:

from pandas import HDFStore

store = HDFStore('file.h5')

last_index = store.select('dataset', where='index == -1').index

除了在我的例子中,最后一个索引不是 -1 而是 Timestamp

最佳答案

使用像位置索引器一样工作的start=stop= 参数

In [8]: df = DataFrame({'A' : np.random.randn(10000)},index=pd.date_range('20130101',periods=10000,freq='s'))

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

In [10]: store.append('df',df)

In [11]: nrows = store.get_storer('df').nrows

In [12]: nrows
Out[12]: 10000

In [13]: store.select('df',start=nrows-1,stop=nrows)
Out[13]: 
                            A
2013-01-01 02:46:39 -0.890721

In [15]: df.iloc[[-1]]
Out[15]: 
                            A
2013-01-01 02:46:39 -0.890721

关于python - 获取 pandas HDF5 查询中的最后一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30515599/

相关文章:

python - 将 pandas 数据框保存到 csv 文件时的附加列

HDF5: "file buffer"和 "file cache"之间有什么区别?

python - 在 pandas 中使用 blosc 压缩会导致堆损坏

python - 查找 HDF5 数据集中的唯一列

python - 将数据加载到Mysql中的表中

python - 在holoviews桑基标签或悬停信息中显示附加值

Python:如何统计实例变量的访问次数

python - 在 Pandas 系列中查找元素的索引

python - mpld3 的 pip 安装

python - 如何解析字符串并返回嵌套数组?