简体   繁体   中英

Convert Rows Category into Columns

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM