Convert categorical data in pandas dataframe

后端 未结 10 1883
予麋鹿
予麋鹿 2020-11-27 10:01

I have a dataframe with this type of data (too many columns):

col1        int64
col2        int64
col3        category
col4        category
col5        categ         


        
10条回答
  •  -上瘾入骨i
    2020-11-27 10:25

    One of the simplest ways to convert the categorical variable into dummy/indicator variables is to use get_dummies provided by pandas. Say for example we have data in which sex is a categorical value (male & female) and you need to convert it into a dummy/indicator here is how to do it.

    tranning_data = pd.read_csv("../titanic/train.csv")
    features = ["Age", "Sex", ] //here sex is catagorical value
    X_train = pd.get_dummies(tranning_data[features])
    print(X_train)
    
    Age Sex_female Sex_male
    20    0          1
    33    1          0
    40    1          0
    22    1          0
    54    0          1

提交回复
热议问题