小编Bri*_*eph的帖子

如何优雅地对熊猫中的一系列列表进行热编码

所以我有以下数据:

>>> test = pd.Series([['a', 'b', 'e'], ['c', 'a'], ['d'], ['d'], ['e']])
>>> test

0    [a, b, e]
1       [c, a]
2          [d]
3          [d]
4          [e]
Run Code Online (Sandbox Code Playgroud)

我正在尝试将列表中的所有数据单热编码回我的数据帧。看起来像这样:

>>> pd.DataFrame([[1, 1, 0, 0, 1], [1, 0, 1, 0, 0],
              [0, 0, 0, 1, 0], [0, 0, 0, 1, 0],
              [0, 0, 0, 0, 1]],
             columns = ['a', 'b', 'c', 'd', 'e'])

    a   b   c   d   e
0   1   1   0   0   1
1   1   0   1   0   0
2   0 …
Run Code Online (Sandbox Code Playgroud)

python python-2.7 python-3.x pandas

8
推荐指数
1
解决办法
4311
查看次数

标签 统计

pandas ×1

python ×1

python-2.7 ×1

python-3.x ×1