Reputation: 556
I have an array of categories as follow.
mappingCategory = ["Animal", "Plant", "Animal", "Human", "Plant"]
Index | Category |
---|---|
0 | Animal |
1 | Plant |
2 | Animal |
3 | Human |
4 | Plant |
I want to convert the array into 2D array where the column is unique category (order by alphabet) and the rows show what category is in that row like this.
mappingCategory = [[1, 0, 0], [0, 0, 1], [1, 0, 0], [0, 1, 0], [0, 0, 1]]
Index | Animal | Human | Plant |
---|---|---|---|
0 | 1 | 0 | 0 |
1 | 0 | 0 | 1 |
2 | 1 | 0 | 0 |
3 | 0 | 1 | 0 |
4 | 0 | 0 | 1 |
I can build my own function to convert it, but is there any python build in function I could use?
Upvotes: 0
Views: 229
Reputation: 23237
You can use .str.get_dummies()
, as follows:
If you have a dataframe column named Category
, use:
df['Category'].str.get_dummies()
Or, if you do it directly from the array of categories:
mappingCategory = ["Animal", "Plant", "Animal", "Human", "Plant"]
pd.Series(mappingCategory).str.get_dummies()
Output:
Animal Human Plant
0 1 0 0
1 0 0 1
2 1 0 0
3 0 1 0
4 0 0 1
Upvotes: 2