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

前端 未结 8 1655
清酒与你
清酒与你 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:37

    A simple & elegant way to do the same.

    cat_list = ['Sun', 'Sun', 'Wed', 'Mon', 'Mon']
    encoded_data, mapping_index = pd.Series(cat_list).factorize()
    

    and you are done, check below

    print(encoded_data)
    print(mapping_index)
    print(mapping_index.get_loc("Mon"))
    

提交回复
热议问题