gpt4 book ai didi

python - 解决python中的pd.get_dummies功能障碍

转载 作者:行者123 更新时间:2023-11-28 21:39:52 24 4
gpt4 key购买 nike

我有

 a={0: ['I3925'], 1: ['I3925'], 2: ['I3925'], 3: ['I2355'], 4: ['I2355'], 5: ['I2355'], 6: ['I111'], 7: ['I111'], 8: ['I111'], 9: ['I405'], 10: ['I405'], 11: ['I3878', 'I2864'], 12: ['I3878'], 13: ['I534'], 14: ['I534'], 15: ['I134', 'I2276'], 16: ['I107'], 17: ['I107'], 18: ['I2864']}  

其中包含一把 key 的一个补充 I 编号。

b = pd.Series(a,\
index = a.keys(),
name = "a")
pd.get_dummies(b.apply(pd.Series))

然后 get_dummies 不起作用,因为它创建了一个重复的列 1_15 来存储与第二个 I 编号的匹配,而不是将它们堆叠到同一列中。我不明白为什么。

    0_I107  0_I111  0_I134  0_I2355 0_I2864 0_I3878 0_I3925 0_I405  0_I534  1_I2276 1_I2864
0 0 0 0 0 0 0 1 0 0 0 0
1 0 0 0 0 0 0 1 0 0 0 0
2 0 0 0 0 0 0 1 0 0 0 0
3 0 0 0 1 0 0 0 0 0 0 0
4 0 0 0 1 0 0 0 0 0 0 0
5 0 0 0 1 0 0 0 0 0 0 0
6 0 1 0 0 0 0 0 0 0 0 0
7 0 1 0 0 0 0 0 0 0 0 0
8 0 1 0 0 0 0 0 0 0 0 0
9 0 0 0 0 0 0 0 1 0 0 0
10 0 0 0 0 0 0 0 1 0 0 0
11 0 0 0 0 0 1 0 0 0 0 1
12 0 0 0 0 0 1 0 0 0 0 0
13 0 0 0 0 0 0 0 0 1 0 0
14 0 0 0 0 0 0 0 0 1 0 0
15 0 0 1 0 0 0 0 0 0 1 0
16 1 0 0 0 0 0 0 0 0 0 0
17 1 0 0 0 0 0 0 0 0 0 0
18 0 0 0 0 1 0 0 0 0 0 0

谁能解释一下我做错了什么?

最佳答案

选项 1

from sklearn.preprocessing import MultiLabelBinarizer

mlb = MultiLabelBinarizer()
pd.DataFrame(mlb.fit_transform(b), b.index, mlb.classes_)

I107 I111 I134 I2276 I2355 I2864 I3878 I3925 I405 I534
0 0 0 0 0 0 0 0 1 0 0
1 0 0 0 0 0 0 0 1 0 0
2 0 0 0 0 0 0 0 1 0 0
3 0 0 0 0 1 0 0 0 0 0
4 0 0 0 0 1 0 0 0 0 0
5 0 0 0 0 1 0 0 0 0 0
6 0 1 0 0 0 0 0 0 0 0
7 0 1 0 0 0 0 0 0 0 0
8 0 1 0 0 0 0 0 0 0 0
9 0 0 0 0 0 0 0 0 1 0
10 0 0 0 0 0 0 0 0 1 0
11 0 0 0 0 0 1 1 0 0 0
12 0 0 0 0 0 0 1 0 0 0
13 0 0 0 0 0 0 0 0 0 1
14 0 0 0 0 0 0 0 0 0 1
15 0 0 1 1 0 0 0 0 0 0
16 1 0 0 0 0 0 0 0 0 0
17 1 0 0 0 0 0 0 0 0 0
18 0 0 0 0 0 1 0 0 0 0

选项 2

b.str.join('|').str.get_dummies()

I107 I111 I134 I2276 I2355 I2864 I3878 I3925 I405 I534
0 0 0 0 0 0 0 0 1 0 0
1 0 0 0 0 0 0 0 1 0 0
2 0 0 0 0 0 0 0 1 0 0
3 0 0 0 0 1 0 0 0 0 0
4 0 0 0 0 1 0 0 0 0 0
5 0 0 0 0 1 0 0 0 0 0
6 0 1 0 0 0 0 0 0 0 0
7 0 1 0 0 0 0 0 0 0 0
8 0 1 0 0 0 0 0 0 0 0
9 0 0 0 0 0 0 0 0 1 0
10 0 0 0 0 0 0 0 0 1 0
11 0 0 0 0 0 1 1 0 0 0
12 0 0 0 0 0 0 1 0 0 0
13 0 0 0 0 0 0 0 0 0 1
14 0 0 0 0 0 0 0 0 0 1
15 0 0 1 1 0 0 0 0 0 0
16 1 0 0 0 0 0 0 0 0 0
17 1 0 0 0 0 0 0 0 0 0
18 0 0 0 0 0 1 0 0 0 0

关于python - 解决python中的pd.get_dummies功能障碍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46245124/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com