【问题标题】:correct query in PyTablesPyTables 中的正确查询
【发布时间】:2014-03-07 09:56:26
【问题描述】:

我正在使用 Python 2.7,并尝试执行 PyTables 查询:

#Here the condition
selectedIndex = [1,6,7,9]
condition = 'IndexColumn in selectedIndex'

#here the query
for x1 in tab.where(condition,selectedIndex):
   ...
   ...
   ...

我得到了:

TypeError: argument of type 'VariableNode' is not iterable

从有关 pytables 的文档中,我正在尝试使用它:

Table.where(condition, condvars=None, start=None, stop=None, step=None)

还有:

condition = 'col1 == "AAAA"'

for record in table.where(condition):  # TypeError in Python3
    #do something with "record"

我做错了什么?

【问题讨论】:

  • 您是否尝试过改用DataFrame.query 方法?我不认为where 接受字符串参数...

标签: python performance pytables


【解决方案1】:

我认为在您的情况下使用字符串参数进行内核查询的正确方法是:

conditon="(IndexColumn==1)|(IndexColumn==6)|(IndexColumn==7)|(IndexColumn==9)"

并且 IndexColumn 需要是您的 IsDescription 类中指定的列的实际名称。我相信 python 条件“a in b”对 Pytablles 查询无效

编辑您的评论:

"".join(["(IndexColumn==%i)|"%j for j in selectedIndex])[:-1]

应该为您提供正确的查询字符串。

【讨论】:

  • 那么这是一个问题,因为如果我需要声明每个条件,我不能发送整个selectedIndex 做类似def processTable(selectedIndex) 的事情,对吗?
  • 不,您可以将适当的字符串与以下内容连接起来:''.join(["(IndexColumn==%i)|"%j for j in selectedIndex])[:-1]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-20
  • 1970-01-01
  • 2014-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多