DolphinDB Python API: Get the number of records queried by SQL statements

58 Views Asked by At

I run the following script in Python:

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

Output (of DataFrame type):

   count
0  13136

If I run the script as follows:

t2.toDF().count # count is the column name corresponding to the result of count(*)

Output (of <class 'method'> data type):

<bound method DataFrame.count of    count
0  13136>

How to get the number of records of type scalar?

2

There are 2 best solutions below

0
Polly On BEST ANSWER

Solution 1: use function pandas.DataFrame.count.

trade.select("*").toDF().count()["col"]
# col is the name of any column of the DataFrame returned by the SQL query
# Any SQL statement can be used before toDF()

Solution 2:

In DolphinDB, the output of t2=t1.select("count(*)") is stored in the “count“ column. t2.toDF().count does not return the result of the count column as there is a method count with the same name in pandas.DataFrame. It is recommended to specify aliases for the result columns returned by count(*).

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

Return a Seires. You can obtain the scalar with index.

t2.toDF().cnt[0]

See also: DolphinDB Python API

0
Hanwei Tang On

The toDF() function returns a DataFrame, please try t2.toDF()['count'][0].