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

多级索引结果

中等 22-pandas-only pandas 专属 multiindexgroupbysize

统计每个 (channel, status) 组合的订单数,结果以两级索引的形式交付。

  • 结果的 index 必须是两级 MultiIndex,层级名依次为 channel、status。
  • 计数放在名为 n 的列里(可以交一个名为 n 的 Series,也可以交只有 n 一列的 DataFrame)。
  • channel 为 null 的订单也要单独成一组,不能丢掉。
  • 只输出实际出现过的组合。

输入

  • orders(order_id, customer_id, order_ts, status, channel)

输出

index 为 [channel, status],值列为 n。行顺序不限。

输入表 (case base,共 2 个 case:base、small)

orders shop/orders
#order_idint64customer_idint64order_tsdatetimestatusstringchannelstring
1 1001 39 2026-01-01 19:38:00 paid app
2 1002 16 2026-01-17 14:07:00 paid app
3 1003 4 2026-01-08 15:40:20 cancelled web
4 1004 2 2026-05-23 10:48:58 paid null
5 1005 12 2026-06-22 11:07:22 paid store
6 1006 17 2026-08-15 05:15:25 paid web
7 1007 9 2026-06-11 01:00:32 refunded store
8 1008 45 2026-09-19 20:47:10 paid web
9 1009 2 2025-10-05 04:53:38 paid app
10 1010 4 2026-02-19 01:39:54 paid app
11 1011 28 2026-03-05 03:04:45 refunded store
12 1012 2 2026-08-01 07:09:17 paid web
13 1013 26 2026-01-22 03:48:14 refunded store
14 1014 2 2026-02-09 01:41:41 paid app
15 1015 5 2026-08-03 19:49:04 refunded web
16 1016 26 2026-02-16 00:43:43 refunded app
17 1017 9 2025-12-06 04:48:57 pending app
18 1018 999 2026-05-19 12:13:43 paid store
19 1019 26 2026-02-05 15:02:26 paid store
20 1020 44 2025-11-24 11:19:00 cancelled web
显示前 20 行,共 200 行

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

#channelStringstatusStringnInt64
1 app cancelled 12
2 app paid 45
3 app pending 6
4 app refunded 8
5 phone cancelled 1
6 phone paid 7
7 phone pending 1
8 store cancelled 3
9 store paid 19
10 store pending 4
11 store refunded 6
12 web cancelled 13
13 web paid 38
14 web pending 3
15 web refunded 15
16 null cancelled 2
17 null paid 13
18 null refunded 4
共 18 行