如何在包含多个值的一列中使用 One-Hot 值
How to One-Hot value in one column where it contains multiple value
如何将值拆分为列并将 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
您可以使用 get_dummies dataframes str 方法的函数,如下所示
pd.concat([df, df.data.str.get_dummies(sep=",")], axis=1)
如何将值拆分为列并将 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
您可以使用 get_dummies dataframes str 方法的函数,如下所示
pd.concat([df, df.data.str.get_dummies(sep=",")], axis=1)