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

宽转长

简单 17-reshape 重塑 unpivotmeltreshapewide-to-long

把日线行情的宽表转成长表:每个 (ticker, date) 的 open, high, low, close 四个价格各占一行。

  • field 列是原来的列名(open / high / low / close 之一),value 列是对应的价格。
  • 价格为 null 的也要保留一行,value 为 null。所以输出行数恰好是输入行数的 4 倍。
  • volume 不参与转换,也不输出。

输入

  • prices(ticker, date, open, high, low, close, volume)

输出

列为 ticker, date, field, value,行顺序不限。

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

prices stocks/prices
#tickerstringdatedateopenfloat64highfloat64lowfloat64closefloat64volumeint64
1 AAPL 2026-06-01 189.58 190.97 188.93 190.66 7374313
2 AAPL 2026-06-02 191.71 195.6 191.42 194.12 1753601
3 AAPL 2026-06-03 194.95 196.13 193.04 193.67 2817746
4 AAPL 2026-06-04 194.03 194.88 187.39 189.08 2783341
5 AAPL 2026-06-05 189.72 193.66 189.02 193.14 8942829
6 AAPL 2026-06-08 192.9 193.62 188.21 188.64 1357786
7 AAPL 2026-06-09 188.63 189.8 185.9 188.98 5482117
8 AAPL 2026-06-10 189.78 191.79 188.78 191.45 1430484
9 AAPL 2026-06-11 191.12 193.18 190.85 192.68 7642427
10 AAPL 2026-06-12 191.04 193.87 190.02 193.37 2476587
11 AAPL 2026-06-15 193.53 195.45 193.24 193.68 1680820
12 AAPL 2026-06-16 194.69 195.53 192.04 193.09 4196575
13 AAPL 2026-06-17 192.64 196.25 191.03 195.73 1464728
14 AAPL 2026-06-18 195.19 196.94 192.23 192.74 2059908
15 AAPL 2026-06-19 192.02 196.01 191.78 195.55 8247165
16 AAPL 2026-06-22 195.28 196.64 188.45 null 2043265
17 AAPL 2026-06-23 190.64 191.82 190.11 191.55 2220638
18 AAPL 2026-06-24 192.95 193.43 189.2 190.21 2962146
19 AAPL 2026-06-25 190.43 190.7 188.71 189.26 1607246
20 AAPL 2026-06-26 189.15 189.58 188.4 189.15 4162289
显示前 20 行,共 296 行

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

#tickerStringdateDatefieldStringvalueFloat64
1 AAPL 2026-06-01 open 189.58
2 AAPL 2026-06-01 high 190.97
3 AAPL 2026-06-01 low 188.93
4 AAPL 2026-06-01 close 190.66
5 AAPL 2026-06-02 open 191.71
6 AAPL 2026-06-02 high 195.6
7 AAPL 2026-06-02 low 191.42
8 AAPL 2026-06-02 close 194.12
9 AAPL 2026-06-03 open 194.95
10 AAPL 2026-06-03 high 196.13
11 AAPL 2026-06-03 low 193.04
12 AAPL 2026-06-03 close 193.67
13 AAPL 2026-06-04 open 194.03
14 AAPL 2026-06-04 high 194.88
15 AAPL 2026-06-04 low 187.39
16 AAPL 2026-06-04 close 189.08
17 AAPL 2026-06-05 open 189.72
18 AAPL 2026-06-05 high 193.66
19 AAPL 2026-06-05 low 189.02
20 AAPL 2026-06-05 close 193.14
21 AAPL 2026-06-08 open 192.9
22 AAPL 2026-06-08 high 193.62
23 AAPL 2026-06-08 low 188.21
24 AAPL 2026-06-08 close 188.64
25 AAPL 2026-06-09 open 188.63
26 AAPL 2026-06-09 high 189.8
27 AAPL 2026-06-09 low 185.9
28 AAPL 2026-06-09 close 188.98
29 AAPL 2026-06-10 open 189.78
30 AAPL 2026-06-10 high 191.79
31 AAPL 2026-06-10 low 188.78
32 AAPL 2026-06-10 close 191.45
33 AAPL 2026-06-11 open 191.12
34 AAPL 2026-06-11 high 193.18
35 AAPL 2026-06-11 low 190.85
36 AAPL 2026-06-11 close 192.68
37 AAPL 2026-06-12 open 191.04
38 AAPL 2026-06-12 high 193.87
39 AAPL 2026-06-12 low 190.02
40 AAPL 2026-06-12 close 193.37
41 AAPL 2026-06-15 open 193.53
42 AAPL 2026-06-15 high 195.45
43 AAPL 2026-06-15 low 193.24
44 AAPL 2026-06-15 close 193.68
45 AAPL 2026-06-16 open 194.69
46 AAPL 2026-06-16 high 195.53
47 AAPL 2026-06-16 low 192.04
48 AAPL 2026-06-16 close 193.09
49 AAPL 2026-06-17 open 192.64
50 AAPL 2026-06-17 high 196.25
显示前 50 行,共 1184 行