如何将包含三列的表转换为哑变量形式?

转换前:

attachments-2022-09-LZ4maf6w631b030ec8cfd.png

转换后:

attachments-2022-09-AJi1qR5L631b031c0ae47.png

请先 登录 后评论

1 个回答

Juntao Wang

我们给出了两种示例方法,推荐第二种,实现简单、性能更好。

模拟数据:

t = table(2022.08.01 2022.08.02 2022.08.03 as date, `000001.SZ`000002.SZ`000003.SZ as windCode, `1101`1102`1103 as industryCode)
方案1:
res = select iif(isNull(industryCode), 0, 1) from t pivot by date, windCode, industryCode
nullFill!(res, 0)

方案2:

oneHot(t, `industryCode)


第2种方案用的One Hot编码方式,关于One Hot编码详见:How can I one hot encode in Python?

请先 登录 后评论