我使用Python-cassandra驱动程序连接和查询cassandra集群。在
我想通过Pandas操作我的数据,在cassandra驱动程序的文档中有一个区域提到了这一点: https://datastax.github.io/python-driver/api/cassandra/protocol.html
NumpyProtocolHander: deserializes results directly into NumPy arrays. This facilitates efficient integration with analysis toolkits such as Pandas.
按照上述说明并在Cassandra中执行SELECT查询,我可以看到输出(通过type()函数)为:
<class 'cassandra.cluster.ResultSet'>
迭代结果,打印一行的结果如下:
^{pr2}$(我限制了查询结果,我使用的数据量要大得多,因此希望使用numpy和pandas)。在
我对熊猫的了解有限,我尝试运行非常基本的功能:
rslt = cassandraSession.execute("SELECT accepted FROM table")
test = rslt[["accepted"]].head(1)
这将输出以下错误:
Traceback (most recent call last):
File "/UserStats.py", line 27, in <module>
test = rslt[["accepted"]].head(1)
File "cassandra/cluster.py", line 3380, in cassandra.cluster.ResultSet.__getitem__ (cassandra/cluster.c:63998)
TypeError: list indices must be integers, not list
我理解这个错误,我只是不知道如何从这个假定的numpy数组“过渡”到能够使用熊猫。在
简单的回答是:
rslt[0]将数据作为Python dict提供给您,可以很容易地将其转换为Pandas数据帧。在
完整的解决方案:
^{pr2}$注意:如果查询很大,上述解决方案只会获取部分数据。所以你应该:
相关问题 更多 >
编程相关推荐