[英]How to split pandas dataframe column to matrix of occurrences
I want to divide column of lists to x (number of unique values) columns with 1 if exists in row and 0 if it doesn't For example:我想将列表的列划分为 x(唯一值的数量)列,如果行中存在则为 1,如果不存在则为 0 例如:
animals
0 dog;mouse;cat
1 cat;dog
2 dog
3 mouse;cat
4 dog;cat
to:至:
dog mouse cat
0 1 1 1
1 1 0 1
2 1 0 0
3 0 1 1
4 1 0 1
Use pandas.Series.str.get_dummies
:使用
pandas.Series.str.get_dummies
:
df['animals'].str.get_dummies(sep=';')
output: output:
cat dog mouse
0 1 1 1
1 1 1 0
2 0 1 0
3 1 0 1
4 1 1 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.