[英]How to convert integer values starting from 1 into one hot values?
I have a pandas dataframe with one column containing values from 1 to 7. How can I convert those values into one hot encoded values? 我有一个熊猫数据框,其中的一列包含从1到7的值。如何将这些值转换为一个热编码值?
As far as I know, starting from 0 up to any values without any break one can use OneHotEncoder of SKlearn but since the numbers start from 1, I couldn't do it. 据我所知,从0到任何值都可以不间断地使用SKlearn的OneHotEncoder,但是由于数字从1开始,所以我做不到。 This column is my dependent feature column for a classification problem. 此列是我有关分类问题的从属要素列。
You can use pandas get_dummies
function ( docs ) 您可以使用pandas get_dummies
函数( docs )
>>> import pandas as pd
>>> df = pd.DataFrame({'numerical_thing':[1,2,3,2,1,1,3,4,5,2,3,2,1]})
>>> pd.get_dummies(df.loc[:,'numerical_thing'])
1 2 3 4 5
0 1 0 0 0 0
1 0 1 0 0 0
2 0 0 1 0 0
3 0 1 0 0 0
4 1 0 0 0 0
5 1 0 0 0 0
6 0 0 1 0 0
7 0 0 0 1 0
8 0 0 0 0 1
9 0 1 0 0 0
10 0 0 1 0 0
11 0 1 0 0 0
12 1 0 0 0 0
Hope that helps. 希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.