Implementation:Eventual Inc Daft DataFrame Count Rows
| Knowledge Sources | |
|---|---|
| Domains | Data_Engineering, Data_Analysis |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
Concrete tool for counting the total number of rows in a DataFrame provided by the Daft library.
Description
The count_rows method on a Daft DataFrame triggers execution to compute and return the exact row count as an integer. If the DataFrame has already been materialized (collected), the count is computed directly from the cached result using len(). Otherwise, a count aggregation plan is built that produces a single-partition, single-row DataFrame with a "count" column. The integer value is extracted from this result and returned. This is more efficient than collecting the full DataFrame just to count rows.
Usage
Use this method on a DataFrame when you need to know the exact number of rows. Note that this triggers execution of the DataFrame's logical plan.
Code Reference
Source Location
- Repository: Daft
- File:
daft/dataframe/dataframe.py - Lines: L2738-2760
Signature
def count_rows(self) -> int
Import
# Method on DataFrame, no separate import needed
n = df.count_rows()
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| self | DataFrame | Yes | The DataFrame to count rows in (implicit) |
Outputs
| Name | Type | Description |
|---|---|---|
| return | int | The total number of rows in the DataFrame |
Usage Examples
Basic Usage
import daft
df = daft.from_pydict({"x": [1, 2, 3], "y": [4, 5, 6], "z": [7, 8, 9]})
n = df.count_rows()
print(n) # 3
# Count rows after filtering
filtered_df = df.where(df["x"] > 1)
print(filtered_df.count_rows()) # 2