df-drills pandas · Polars · DuckDB SQL · DuckDB Relational 题目列表 登录

LazyFrame 管道

简单 23-polars-only Polars 专属 lazyscanio

工作目录里有一个事件文件 data/events.parquet,列为 event_id, user_id, ts, event_type, page。

统计每个用户的购买次数(event_type = 'purchase' 的事件数)。

  • result 必须是一个 LazyFrame:从文件开始就用惰性方式构建查询,不要 collect,判题器会替你 collect。
  • 只输出至少有一次购买的用户。

输出

列为 user_id, n_purchases,行顺序不限。

工作目录里的文件 (case base)

data/events.parquet
#event_idInt64user_idInt64tsDatetime(time_unit='us', time_zone=None)event_typeStringpageString
1 1 53 2026-06-03 17:57:33 login /home
2 2 53 2026-06-04 15:21:00 visit /home
3 3 53 2026-06-04 15:22:00 view /product/303
4 4 53 2026-06-04 15:32:04 add_to_cart /cart
5 5 56 2026-06-05 23:24:19 login /home
6 6 56 2026-06-05 23:29:18 view /product/202
7 7 56 2026-06-05 23:36:15 view /product/202
8 8 56 2026-06-05 23:47:36 add_to_cart /cart
9 9 56 2026-06-05 23:56:24 checkout /checkout
10 10 56 2026-06-06 19:14:00 visit /home
11 11 56 2026-06-06 19:22:53 view /product/303
12 12 56 2026-06-06 19:25:12 view /home
13 13 56 2026-06-06 19:28:25 add_to_cart /cart
14 14 29 2026-06-07 10:40:01 login /home
15 15 29 2026-06-07 10:46:43 view /product/303
16 16 29 2026-06-07 11:04:46 add_to_cart /cart
17 17 29 2026-06-07 11:09:25 checkout /checkout
18 18 34 2026-06-07 12:57:49 login /home
19 19 34 2026-06-07 13:03:57 view /product/202
20 20 34 2026-06-07 13:11:07 view /home
显示前 20 行,共 1500 行

期望输出 (case base,行顺序不限)

#user_idInt64n_purchasesUInt32
1 26 6
2 48 4
3 39 3
4 29 3
5 59 2
6 45 3
7 16 2
8 28 3
9 57 1
10 31 2
11 15 1
12 22 1
13 33 1
14 18 2
15 21 2
16 5 1
17 58 1
18 56 2
19 11 4
20 35 1
21 17 2
22 23 1
23 54 2
24 25 2
25 43 1
26 9 1
27 3 1
28 55 1
29 4 6
30 42 2
31 47 7
32 14 1
33 50 3
34 8 2
35 49 1
36 7 3
37 36 4
38 32 2
39 20 2
40 30 1
41 46 3
42 34 1
43 41 4
44 6 1
45 60 2
46 19 1
47 13 1
48 12 1
共 48 行