在 KDB 中将单列拆分为多列
Split single column to multiple columns in KDB
我想在应用复杂函数后将一列中的值拆分为多列。
例如对于以下交易 table t ,我想将 sym 拆分为 2 个单独的列 sym 和 src。但是,我要应用的功能会稍微复杂一些。
q)t:([] time:10:01:01 10:01:03 10:01:04;sym:`goog.l`vod.l`apple.o;qty:100 200 150)
time sym qty
--------------------
10:01:01 goog.l 100
10:01:03 vod.l 200
10:01:04 apple.o 150
完成此操作的方法之一是:
q)update sym:sym[;0] , mkt:sym[;1] from update ` vs/:sym from t
time sym qty mkt
----------------------
10:01:01 goog 100 l
10:01:03 vod 200 l
10:01:04 apple 150 o
如果您对除 table 中需要拆分的列以外的任何其他列不感兴趣,那么
q)exec {`s`mkt!` vs x}each sym from t
s mkt
---------
goog l
vod l
apple o
您可以创建 sym
和 src
的 table,方法是拆分 .
,创建字典,然后使用 flip
创建 table:
q)show r:exec flip`sym`src!flip` vs/:sym from t
sym src
---------
goog l
vod l
apple o
这可以加入原来的 table 使用 each-both ,'
:
q)t,'r
time sym qty src
----------------------
10:01:01 goog 100 l
10:01:03 vod 200 l
10:01:04 apple 150 o
如果列顺序很重要,那么可以使用 xcols
:
来解决这个问题
q)`time`sym`src xcols t,'r
time sym src qty
----------------------
10:01:01 goog l 100
10:01:03 vod l 200
10:01:04 apple o 150
如果您的 table 非常大并且 sym 列非常重复(如果它是分时数据,它看起来会如此)那么下面的操作会更快:
f:{` vs'x}
@[t;`col1`col2;:;flip .Q.fu[f]t`sym]
另一种选择是;
q)(,'/)(t;flip`sym`src!exec flip ` vs'sym from t)
time sym qty src
----------------------
10:01:01 goog 100 l
10:01:03 vod 200 l
10:01:04 apple 150 o
我想在应用复杂函数后将一列中的值拆分为多列。
例如对于以下交易 table t ,我想将 sym 拆分为 2 个单独的列 sym 和 src。但是,我要应用的功能会稍微复杂一些。
q)t:([] time:10:01:01 10:01:03 10:01:04;sym:`goog.l`vod.l`apple.o;qty:100 200 150)
time sym qty
--------------------
10:01:01 goog.l 100
10:01:03 vod.l 200
10:01:04 apple.o 150
完成此操作的方法之一是:
q)update sym:sym[;0] , mkt:sym[;1] from update ` vs/:sym from t
time sym qty mkt
----------------------
10:01:01 goog 100 l
10:01:03 vod 200 l
10:01:04 apple 150 o
如果您对除 table 中需要拆分的列以外的任何其他列不感兴趣,那么
q)exec {`s`mkt!` vs x}each sym from t
s mkt
---------
goog l
vod l
apple o
您可以创建 sym
和 src
的 table,方法是拆分 .
,创建字典,然后使用 flip
创建 table:
q)show r:exec flip`sym`src!flip` vs/:sym from t
sym src
---------
goog l
vod l
apple o
这可以加入原来的 table 使用 each-both ,'
:
q)t,'r
time sym qty src
----------------------
10:01:01 goog 100 l
10:01:03 vod 200 l
10:01:04 apple 150 o
如果列顺序很重要,那么可以使用 xcols
:
q)`time`sym`src xcols t,'r
time sym src qty
----------------------
10:01:01 goog l 100
10:01:03 vod l 200
10:01:04 apple o 150
如果您的 table 非常大并且 sym 列非常重复(如果它是分时数据,它看起来会如此)那么下面的操作会更快:
f:{` vs'x}
@[t;`col1`col2;:;flip .Q.fu[f]t`sym]
另一种选择是;
q)(,'/)(t;flip`sym`src!exec flip ` vs'sym from t)
time sym qty src
----------------------
10:01:01 goog 100 l
10:01:03 vod 200 l
10:01:04 apple 150 o