python-3.x - 如何在一列中包含多个值的 One-Hot 值
问题描述
如果记录包含值,我如何将值拆分为列并将 1 设置为记录
数据集创建
df = pd.DataFrame({
"date": ['1-1-2019', '1-2-2019'],
"data": ['abc,bcd','abc,efg,hij'],
"Others" :['Other column info','Other column info']
})
原始数据
date data Others
1-1-2019 abc,bcd Other column info
1-2-2019 abc,efg,hij Other column info
预期结果
date abc bcd efg hij Others
1-1-2019 1 1 0 0 Other column info
1-2-2019 1 0 1 1 Other column info
解决方案
您可以使用数据帧 str 方法的 get_dummies 函数,如下所示
pd.concat([df, df.data.str.get_dummies(sep=",")], axis=1)
推荐阅读
- algorithm - 在有向图中查找可达顶点
- cuda - cuda 从设备内存中复制内核中动态 malloc 的数据
- sapui5 - sap.m.TablePersoController 多表个性化
- system-verilog - SystemVerilog 中单位常量文字的符号扩展
- python - 按其他数据框列过滤熊猫行
- xml - 如何使用 powershell 转换 XML 中的属性?
- php - 如何多次更新行laravel
- c# - SqlServer2012-错误 40-无法在 SQL Server 中打开连接
- vim - Vim 符号:使用 RltvNmbr 脚本的 Linter 指示
- c# - c# odata (v4) - power bi 报告:发现意外的“StartArray”节点