如何在kdb中对多个复合列使用ungroup [英] how to use ungroup with multiple composite columns in kdb

查看:68
本文介绍了如何在kdb中对多个复合列使用ungroup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑下表:

`Keys  `Values
`A`B   `V1`V2
`C`D`E   `V1`V2`V3`V4

我只想展平Keys列,以便每个键都映射到相应的Values.结果应该是:

I want to flatten the Keys column only such that each key is mapped to the corresponding Values. The result should be:

`Keys  `Values
`A     `V1`V2
`B     `V1`V2
`C     `V1`V2`V3`V4
`D     `V1`V2`V3`V4
`E     `V1`V2`V3`V4

ungroup函数将ungroup应用于所有列,在这种情况下,我想仅应用于Keys列.

ungroup function applies ungroup to all columns, in this case I want to apply to Keys column alone.

推荐答案

您可以通过几种不同的方法来解决此问题,以下是一种方法-

Few different ways you can go about this, below is one way -

ungroupCol:{[tbl;col]
    @[tbl where count each tbl col;col;:;raze tbl col]
}

然后:

q)t:flip `Keys`Values!((`A`B;`C`D`E);(`V1`V2;`V1`V2`V3`V4))
q)ungroupCol[t;`Keys]
    Keys Values
    -----------------
    A    `V1`V2
    B    `V1`V2
    C    `V1`V2`V3`V4
    D    `V1`V2`V3`V4
    E    `V1`V2`V3`V4

这篇关于如何在kdb中对多个复合列使用ungroup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆