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

前端 未结 8 1651
清酒与你
清酒与你 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.

    0 讨论(0)
  • 2021-02-01 15:46

    The best way of doing this can be to use label encoder of sklearn library.

    Something like this:

    from sklearn import preprocessing
    le = preprocessing.LabelEncoder()
    le.fit(["paris", "paris", "tokyo", "amsterdam"])
    list(le.classes_)
    le.transform(["tokyo", "tokyo", "paris"])
    list(le.inverse_transform([2, 2, 1]))
    
    0 讨论(0)
提交回复
热议问题