python api中t1.select("count(*)")的count的值怎么取出

在python中运行下列代码:

t2 = t1.select("count(*)")
t2.toDF().count.tolist()[0]

会报错,  'function' object has no attribute 'tolist'。

 我想计算一下满足条件的记录数,如下图所示输入t2.toDF() 显示是正常的:

attachments-2023-01-igay4z5T63bd55f29f241.png

请先 登录 后评论

1 个回答

wale
trade.select("*").toDF().count()["col"]
# 其中 col 为 SQL 查询返回的 DataFrame 任意一列的名字
# toDF() 前可以替换为任意 SQL 语句
  • 方法二:

在 DolphinDB 中,上述 t2=t1.select("count(*)") 脚本执行结果将保存在名为 "count" 的列里。调用 t2.toDF().count 时,由于 pandas.DataFrame 存在同名的方法 count,因此返回结果不是 count 列的结果。建议给 count(*) 返回的结果列取别名。

t2 = t1.select("count(*) as cnt")
t2.toDF().cnt

该脚本返回一个 Series 类型的结果。若要获取标量,直接通过索引即可:

t2.toDF().cnt[0]

附:[DolphinDB Python API 使用教程](https://github.com/dolphindb/api_python3/blob/master/README_CN.md),以供参考。

请先 登录 后评论