Home > Backend Development > Python Tutorial > How to Convert Pandas Categories to Numerical Indices?

How to Convert Pandas Categories to Numerical Indices?

Susan Sarandon
Release: 2024-10-29 03:44:02
Original
582 people have browsed it

How to Convert Pandas Categories to Numerical Indices?

Converting Pandas Categories to Numerical Indices

Problem:

Given a dataframe containing categorical values, the task is to convert these categories into numerical indices. Suppose we have countries as categories like this:

cc | temp
US | 37.0
CA | 12.0
US | 35.0
AU | 20.0
Copy after login

Instead of one-hot encodings using get_dummies, the goal is to assign each country an index, such as cc_index = [1,2,1,3].

Solution:

To convert Pandas categories to numerical indices, follow these steps:

  1. Change the data type of the categorical column:

    df.cc = pd.Categorical(df.cc)
    Copy after login
  2. Create a new column to store the category codes:

    df['code'] = df.cc.codes
    Copy after login

This will result in a dataframe with the additional code column containing the numerical indices:

   cc  temp  code
0  US  37.0     2
1  CA  12.0     1
2  US  35.0     2
3  AU  20.0     0
Copy after login

Alternatively, you can utilize the astype method to convert the categorical column directly to a categorical column with codes:

df.cc.astype('category').codes
Copy after login

Another option is to use the categorical column as the index of a new dataframe:

df2 = pd.DataFrame(df.temp)
df2.index = pd.CategoricalIndex(df.cc)
Copy after login

The above is the detailed content of How to Convert Pandas Categories to Numerical Indices?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template