首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在hdfstore中使用OR选择熊猫

在hdfstore中使用OR选择熊猫
EN

Stack Overflow用户
提问于 2013-12-10 18:59:21
回答 1查看 2.2K关注 0票数 4

我知道如何在熊猫HDFStore.select中使用查询和查询类型,但是如何使用OR呢?

例如,我有以下代码

代码语言:javascript
复制
import pandas as pd
df1 = pd.DataFrame({'A': randn(100),
                'B': randn(100),
                'C': randn(100).cumsum()},
                index=pd.bdate_range(end=pd.Timestamp('20131031 23:59:00'), periods=100))
df1.to_hdf('testHDF.h5', 'testVar1', format='table', data_columns=True, append=True)

然后,我可以使用以下方法从这个数据集中部分加载

代码语言:javascript
复制
store = pd.HDFStore('testHDF.h5')
store.select('testVar1', [pd.Term('index', '>=', pd.Timestamp('20131017')), 'A > 0'])

代码语言:javascript
复制
store.select('tableVar2', where=('A > 0', 'B > 0', 'index >= 20131017'))

显然,它使用并组合了我提供的所有标准,例如('A > 0‘和'B > 0’和‘索引>= 20131017')

我的问题是,如何使用OR,例如返回的结果是('A > 0‘OR 'B > 0')?

谢谢你的帮助

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-10 19:11:59

在0.12中,您必须考虑选择多个标准的结果(请记住,您可能会生成重复的标准)

代码语言:javascript
复制
In [9]: pd.concat([store.select('testVar1', where=('A > 0', 'index >= 20131017')),
                   store.select('testVar1', where=('B > 0', 'index >= 20131017'))]).drop_duplicates().sort_index()
Out[9]: 
                   A         B          C
2013-10-17  0.156248  0.085911  10.238636
2013-10-22 -0.125369  0.335910  10.865678
2013-10-23 -2.531444  0.690332  12.335883
2013-10-24 -0.266777  0.501257  13.529781
2013-10-25  0.815413 -0.629418  14.690554
2013-10-28  0.383213 -0.587026  13.589094
2013-10-31  1.897674  0.361764  14.595062

[7 rows x 3 columns]

在0.13/master (0.13rc1退出!)中,您只需执行非常自然的查询即可。

代码语言:javascript
复制
In [10]: store.select('testVar1', where='(A > 0 | B > 0) & index >= 20131017')
Out[10]: 
                   A         B          C
2013-10-17  0.156248  0.085911  10.238636
2013-10-22 -0.125369  0.335910  10.865678
2013-10-23 -2.531444  0.690332  12.335883
2013-10-24 -0.266777  0.501257  13.529781
2013-10-25  0.815413 -0.629418  14.690554
2013-10-28  0.383213 -0.587026  13.589094
2013-10-31  1.897674  0.361764  14.595062

[7 rows x 3 columns]
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20502996

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档