如何向 kdb 中的每一列添加一个子字符串

How do I adds a substring to every column in kdb

我当前在 kdb 中的列是 (Time;Buy;Sell)。我应该怎么做才能将我的列名称更改为 (Time_hist;Buy_hist;Sell_hist)?

谢谢!

您可以使用 xcol 重命名 kdb 中的列:

q)tab:([] Time:(.z.t-10;.z.t-5;.z.t);Buy:23 35 42;Sell:22 33 40)
q)tab
Time         Buy Sell
---------------------
15:51:50.746 23  22  
15:51:50.751 35  33  
15:51:50.756 42  40  
q)`Time_hist`Buy_hist`Sell_hist xcol tab
Time_hist    Buy_hist Sell_hist
-------------------------------
15:51:50.746 23       22       
15:51:50.751 35       33       
15:51:50.756 42       40

可以在以下位置找到更多文档: https://code.kx.com/q/ref/cols/

以下表达式会将 "_hist" 附加到所有列名

(`$(string cols t),\:"_hist") xcol t

其中 t 是 table。

  1. string cols t - 检索所有列名并将它们转换为字符串
  2. (string cols t),\:"_hist""_hist" 附加到左侧的每个列名
  3. colnames xcol t 重命名 table 列名称。有关详细信息,请参阅 xcol