Any way to get mappings of a label encoder in Python pandas?

前端 未结 8 1656
清酒与你
清酒与你 2021-02-01 15:11

I am converting strings to categorical values in my dataset using the following piece of code.

data[\'weekday\'] = pd.Categorical.from_array(data.weekday).labels         


        
8条回答
  •  情歌与酒
    2021-02-01 15:43

    If you have numerical and categorical both type of data in dataframe You can use : here X is my dataframe having categorical and numerical both variables

    from sklearn import preprocessing
    le = preprocessing.LabelEncoder()
    
    for i in range(0,X.shape[1]):
        if X.dtypes[i]=='object':
            X[X.columns[i]] = le.fit_transform(X[X.columns[i]])
    

    Or you can try this:

    from sklearn.preprocessing import LabelEncoder
    
    le = LabelEncoder()
    data = data.apply(le.fit_transform)
    

    Note: This technique is good if you are not interested in converting them back.

提交回复
热议问题