分类变量到二元变量

时间:2018-05-31 08:57:00

标签: pandas categorical-data data-processing

我有一个如下所示的DataFrame: initial dataframe

我在'Concepts_clean'列中有不同的标签,我想自动填充其他标签:resulting dataframe

例如:第四行,列'Concepts_clean'我有['AccueilAmabilité','Tarifs']然后我想填充'AccueilAmabilité'和'Tarifs'这些列,其中包括零和其他所有零。< / p>

最有效的方法是什么?

谢谢

1 个答案:

答案 0 :(得分:0)

它更像是一个n-hot编码问题 -

>>> def change_df(x):
...  for i in x['Concepts_clean'].replace('[','').replace(']','').split(','):
...   x[i.strip()] = 1
...  return x
...
>>> df.apply(change_df, axis=1)

示例输出

Concepts_clean          Ecoute  Informations  Tarifs
[Tarifs]                 0.0           0.0     1.0
[]                       0.0           0.0     0.0
[Ecoute]                 1.0           0.0     0.0
[Tarifs, Informations]   0.0           1.0     1.0
相关问题