检查Cassandra的resultSet是否为空(Python)

2024-09-28 22:39:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我在做这样的事情

    def searchCassanadra(self):
    # Iterate over every pokemon
    for x in self.pokemon_list:
        # Query
        query = session.execute("SELECT pokemon_id FROM ds_pokedex.pokemon where pokemon_id=" + repr(x))

上面的代码返回给我<class 'cassandra.cluster.ResultSet'>

我如何检查这个ResultSet是空的还是从Cassandra填充的?在

我正在用python编写代码。 抱歉问新手的问题。在

如果我试着

^{pr2}$

我得到了这个错误

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

谢谢你的帮助。在


Tags: 代码inselfidfordefquery事情
1条回答
网友
1楼 · 发布于 2024-09-28 22:39:20

看起来您的query._current_rows属性是pandas数据帧,因此当您尝试运行query.current_rows时,无论ResultSet是否为空,都会引发一个ValueError。在

ResultSetCassandra Driver docs中,我们看到current_rows函数寻找_current_rows属性的存在:

@property
def current_rows(self):
    """
    The list of current page rows. May be empty if the result was empty,
    or this is the last page.
    """
    return self._current_rows or []

如果上面的self._current_rows是pandas数据帧,则始终返回ValueError。例如:

^{pr2}$

因此,要检查ResultSet中的pandas DataFrame是否包含数据,可以执行以下操作:

if not query._current_rows.empty:
        print 'y'
    else:
        print 'n'

(注意:我不知道您的session.row_factory是什么样子,但我假设它是从Cassandra返回的行创建pandas数据帧,类似于this答案)

相关问题 更多 >