我有这样的专栏:

         Genre
Action|Crime|Drama|Thriller
Action|Crime|Thriller
Drama|Thriller
Crime|Drama
Horror|Thriller
Crime|Drama|Mystery|Thriller
Documentary
Comedy|Crime
Action|Adventure|Sci-Fi
.....
so on.


我想要的是输出像多列:

it generate various column of genre eg:
action  scifi crime adventure . . . . .
0       1      0     1     0
1       0      0     0     0

最佳答案

使用.str.splitstackget_dummies

df['Genre'].str.split('|',expand=True).stack().str.get_dummies().sum(level=0)


输出:

   Action  Adventure  Comedy  Crime  Documentary  Drama  Horror  Mystery  \
0       1          0       0      1            0      1       0        0
1       1          0       0      1            0      0       0        0
2       0          0       0      0            0      1       0        0
3       0          0       0      1            0      1       0        0
4       0          0       0      0            0      0       1        0
5       0          0       0      1            0      1       0        1
6       0          0       0      0            1      0       0        0
7       0          0       1      1            0      0       0        0
8       1          1       0      0            0      0       0        0

   Sci-Fi  Thriller
0       0         1
1       0         1
2       0         1
3       0         0
4       0         1
5       0         1
6       0         0
7       0         0
8       1         0

关于python - 如何从单个列获取多个列?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45486164/

10-12 05:30