Python 3.x 如何在包含多个值的列中创建一个热值
如果记录包含值,如何将值拆分为列并将其设置为1 数据集创建Python 3.x 如何在包含多个值的列中创建一个热值,python-3.x,pandas,numpy,etl,data-manipulation,Python 3.x,Pandas,Numpy,Etl,Data Manipulation,如果记录包含值,如何将值拆分为列并将其设置为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,
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
您可以使用dataframes str方法的get_dummies函数,如下所示
pd.concat([df,df.data.str.get_dummies(sep=“,”),轴=1)