Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何在包含多个值的列中创建一个热值_Python 3.x_Pandas_Numpy_Etl_Data Manipulation - Fatal编程技术网

Python 3.x 如何在包含多个值的列中创建一个热值

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,

如果记录包含值,如何将值拆分为列并将其设置为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

您可以使用dataframes str方法的get_dummies函数,如下所示

pd.concat([df,df.data.str.get_dummies(sep=“,”),轴=1)