[Python] Python Pandas OneHotEncoder categories

Discussão em 'Python' iniciado por Stack, Junho 15, 2025.

  1. Stack

    Stack Membro Participativo

    I was reading about One Hot Encoding in Python and there is a line that I cannot explain what it means. The code is the following:

    ohe=preprocessing.OneHotEncoder(dtype=np.int,sparse=True,handle_unknown="ignore")
    data=ohe.fit_transform(df[["country"]])


    The thing is when I print the values of categories like this:

    print (ohe.categories_)


    It prints [array(['EEUU', 'France', 'Portugal', 'Italy'], dtype=object)]

    but when I do this:

    print (ohe.categories_[0])

    ['EEUU', 'France', 'Portugal', 'Italy']


    I was not able to find what [0] does, it seems that it converts from an array to a list, but why not use something like the tolist() function?

    I have searched on the web, but I was not able to find an explanation about this expression.

    Continue reading...

Compartilhe esta Página