在 KDB+ 中将列 table 转换为 year/date
Pivot table with columns as year/date in KDB+
我正在尝试从一个简单的 table
中创建一个列为 year
的数据透视表 table
q)growth:([] stock:asc 9#`goog`apple`nokia; year: 9#2015 2016 2017; returns:9?20 )
q)growth
stock year returns
------------------
apple 2015 9
apple 2016 18
apple 2017 17
goog 2015 8
goog 2016 13
goog 2017 17
nokia 2015 12
nokia 2016 12
nokia 2017 2
但我无法获得正确的结构,它仍然返回给我一个字典而不是多个 year
列。
q)exec (distinct growth`year)#year!returns by stock:stock from growth
stock|
-----| ----------------------
apple| 2015 2016 2017!9 18 17
goog | 2015 2016 2017!8 13 17
nokia| 2015 2016 2017!12 12 2
我做错了什么吗?
您需要将年份转换为符号才能将它们用作列 headers。在这种情况下,我首先更新了 growth
table 然后执行了枢轴:
q)exec distinct[year]#year!returns by stock:stock from update `$string year from growth
stock| 2015 2016 2017
-----| --------------
apple| 12 8 10
goog | 1 9 11
nokia| 5 6 1
此外,您可能会看到我已将 (distinct growth`year)
更改为 distinct[year]
,因为这会产生与从更新的 table 中提取 year
相同的结果。
KDB中table的列名应该是符号而不是任何其他数据类型。
在你的数据透视表 table 中,'year' 列的数据类型是 int\long 这就是正确的 table 没有出现的原因。
如果你输入 cast as symbol,那么它会起作用。
q)growth:([] stock:asc 9#`goog`apple`nokia; year: 9#2015 2016 2017; returns:9?20 )
q)growth:update `$string year from growth
q)exec (distinct growth`year)#year!returns by stock:stock from growth
stock| 2015 2016 2017
-----| --------------
apple| 9 18 17
goog | 8 13 17
nokia| 12 12 2
或者,您可以将数据透视列切换为 'stock' 而不是 'year' 并获得具有相同原始 table 的数据透视表 table。
q)growth:([] stock:asc 9#`goog`apple`nokia; year: 9#2015 2016 2017; returns:9?20 )
q)show exec (distinct growth`stock)#stock!returns by year:year from growth
year| apple goog nokia
----| ----------------
2015| 4 2 4
2016| 5 13 12
2017| 12 6 1
我正在尝试从一个简单的 table
中创建一个列为year
的数据透视表 table
q)growth:([] stock:asc 9#`goog`apple`nokia; year: 9#2015 2016 2017; returns:9?20 )
q)growth
stock year returns
------------------
apple 2015 9
apple 2016 18
apple 2017 17
goog 2015 8
goog 2016 13
goog 2017 17
nokia 2015 12
nokia 2016 12
nokia 2017 2
但我无法获得正确的结构,它仍然返回给我一个字典而不是多个 year
列。
q)exec (distinct growth`year)#year!returns by stock:stock from growth
stock|
-----| ----------------------
apple| 2015 2016 2017!9 18 17
goog | 2015 2016 2017!8 13 17
nokia| 2015 2016 2017!12 12 2
我做错了什么吗?
您需要将年份转换为符号才能将它们用作列 headers。在这种情况下,我首先更新了 growth
table 然后执行了枢轴:
q)exec distinct[year]#year!returns by stock:stock from update `$string year from growth
stock| 2015 2016 2017
-----| --------------
apple| 12 8 10
goog | 1 9 11
nokia| 5 6 1
此外,您可能会看到我已将 (distinct growth`year)
更改为 distinct[year]
,因为这会产生与从更新的 table 中提取 year
相同的结果。
KDB中table的列名应该是符号而不是任何其他数据类型。
在你的数据透视表 table 中,'year' 列的数据类型是 int\long 这就是正确的 table 没有出现的原因。
如果你输入 cast as symbol,那么它会起作用。
q)growth:([] stock:asc 9#`goog`apple`nokia; year: 9#2015 2016 2017; returns:9?20 )
q)growth:update `$string year from growth
q)exec (distinct growth`year)#year!returns by stock:stock from growth
stock| 2015 2016 2017
-----| --------------
apple| 9 18 17
goog | 8 13 17
nokia| 12 12 2
或者,您可以将数据透视列切换为 'stock' 而不是 'year' 并获得具有相同原始 table 的数据透视表 table。
q)growth:([] stock:asc 9#`goog`apple`nokia; year: 9#2015 2016 2017; returns:9?20 )
q)show exec (distinct growth`stock)#stock!returns by year:year from growth
year| apple goog nokia
----| ----------------
2015| 4 2 4
2016| 5 13 12
2017| 12 6 1