Home >Backend Development >Python Tutorial >How to Convert Pandas Categorical Columns to Numerical Indices Without `get_dummies` and `numpy`?
Consider a DataFrame with a categorical column, such as country codes:
cc | temp US | 37.0 CA | 12.0 US | 35.0 AU | 20.0
To convert these categories to indices, avoiding the use of get_dummies and numpy, consider the following steps:
df.cc = pd.Categorical(df.cc)
df['code'] = df.cc.codes
The resulting DataFrame will include a new column called code with 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
Alternatively, you can obtain the category codes without modifying the DataFrame:
df.cc.astype('category').codes
df2 = pd.DataFrame(df.temp) df2.index = pd.CategoricalIndex(df.cc)
The above is the detailed content of How to Convert Pandas Categorical Columns to Numerical Indices Without `get_dummies` and `numpy`?. For more information, please follow other related articles on the PHP Chinese website!