微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我试图得到存储在HDF5中的pandas数据帧的最后一行的索引,而不必将整个数据集或索引拉入内存.我正在寻找这样的东西:

from pandas import hdfstore

store = hdfstore('file.h5')

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

除了在我的情况下,最后一个索引不是-1而是一个时间戳

解决方法:

使用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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐