admin管理员组文章数量:1279207
I have a table like below:
q)tab:([]time:.z.t;sym:`ax`bx`cx`ds;col1:`a`a`b`b;price:1.4,1.2,1.3,1.4)
q)tab
time sym col1 price
---------------------------
20:29:37.309 ax a 1.4
20:29:37.309 bx a 1.2
20:29:37.309 cx b 1.3
20:29:37.309 ds b 1.4
q)
My goal is to change values in col1 from a->b where a and b->a where b and have the same column name. Here is my code below that does the job but is there a better way to achieve this in fewer lines of code?
testTab:update col2:count[i]#enlist `b from tab where col1=`a
testTab:update col2:count[i]#enlist `a from testTab where col1=`b
q)testTab
time sym col1 price col2
--------------------------------
20:29:37.309 ax a 1.4 b
20:29:37.309 bx a 1.2 b
20:29:37.309 cx b 1.3 a
20:29:37.309 ds b 1.4 a
q)testTab:`time`sym`col2 xcols testTab //Bring forward col2
q)testTab
time sym col2 col1 price
--------------------------------
20:29:37.309 ax b a 1.4
20:29:37.309 bx b a 1.2
20:29:37.309 cx a b 1.3
20:29:37.309 ds a b 1.4
q)testTab: delete col1 from testTab //Delete original col1 from the table
q)testTab: `time`sym`col1 xcol testTab // Rename col2 to col1
q)testTab
time sym col1 price
---------------------------
20:29:37.309 ax b 1.4
20:29:37.309 bx b 1.2
20:29:37.309 cx a 1.3
20:29:37.309 ds a 1.4
q)
Thanks!
I have a table like below:
q)tab:([]time:.z.t;sym:`ax`bx`cx`ds;col1:`a`a`b`b;price:1.4,1.2,1.3,1.4)
q)tab
time sym col1 price
---------------------------
20:29:37.309 ax a 1.4
20:29:37.309 bx a 1.2
20:29:37.309 cx b 1.3
20:29:37.309 ds b 1.4
q)
My goal is to change values in col1 from a->b where a and b->a where b and have the same column name. Here is my code below that does the job but is there a better way to achieve this in fewer lines of code?
testTab:update col2:count[i]#enlist `b from tab where col1=`a
testTab:update col2:count[i]#enlist `a from testTab where col1=`b
q)testTab
time sym col1 price col2
--------------------------------
20:29:37.309 ax a 1.4 b
20:29:37.309 bx a 1.2 b
20:29:37.309 cx b 1.3 a
20:29:37.309 ds b 1.4 a
q)testTab:`time`sym`col2 xcols testTab //Bring forward col2
q)testTab
time sym col2 col1 price
--------------------------------
20:29:37.309 ax b a 1.4
20:29:37.309 bx b a 1.2
20:29:37.309 cx a b 1.3
20:29:37.309 ds a b 1.4
q)testTab: delete col1 from testTab //Delete original col1 from the table
q)testTab: `time`sym`col1 xcol testTab // Rename col2 to col1
q)testTab
time sym col1 price
---------------------------
20:29:37.309 ax b 1.4
20:29:37.309 bx b 1.2
20:29:37.309 cx a 1.3
20:29:37.309 ds a 1.4
q)
Thanks!
Share Improve this question asked Feb 24 at 20:37 CleanSockCleanSock 3952 gold badges4 silver badges15 bronze badges1 Answer
Reset to default 3Simplest way is a dictionary/map:
q)map:`a`b!`b`a
q)
q)update map col1 from tab
time sym col1 price
---------------------------
21:01:27.484 ax b 1.4
21:01:27.484 bx b 1.2
21:01:27.484 cx a 1.3
21:01:27.484 ds a 1.4
If not all values are to change (i.e. only a subset are to be remapped) then only the subset that is changing should be in the map dictionary, and the rest can fill (^
) with existing value
update col1^map col1 from tab
本文标签: kdbKDB fewer lines of code for modifying values in a colStack Overflow
版权声明:本文标题:kdb+ - KDB fewer lines of code for modifying values in a col - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741242692a2364287.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论