将表转换为单列值的一键编码 [英] Transform table to one-hot-encoding of single column value
本文介绍了将表转换为单列值的一键编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含两列的表:
I have a table with two columns:
+---------+--------+
| keyword | color |
+---------+--------+
| foo | red |
| bar | yellow |
| fobar | red |
| baz | blue |
| bazbaz | green |
+---------+--------+
我需要在PostgreSQL中进行某种形式的单编码和转换表:
I need to do some kind of one-hot encoding and transform table in PostgreSQL to:
+---------+-----+--------+-------+------+
| keyword | red | yellow | green | blue |
+---------+-----+--------+-------+------+
| foo | 1 | 0 | 0 | 0 |
| bar | 0 | 1 | 0 | 0 |
| fobar | 1 | 0 | 0 | 0 |
| baz | 0 | 0 | 0 | 1 |
| bazbaz | 0 | 0 | 1 | 0 |
+---------+-----+--------+-------+------+
是否可以仅使用SQL?有关入门的任何提示?
Is it possible to do with SQL only? Any tips on how to get started?
推荐答案
如果我正确理解,则需要条件聚合:
If I correctly understand, you need conditional aggregation:
select keyword,
count(case when color = 'red' then 1 end) as red,
count(case when color = 'yellow' then 1 end) as yellow
-- another colors here
from t
group by keyword
这篇关于将表转换为单列值的一键编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文