Bob
Bob

Reputation: 583

Get mapping of categorical variables in pandas

I'm doing this to make categorical variables numbers

>>> df = pd.DataFrame({'x':['good', 'bad', 'good', 'great']}, dtype='category')

       x
0   good
1    bad
2   good
3  great

How can I get the mapping between the original values and the new values?

Upvotes: 51

Views: 73692

Answers (3)

Aray Karjauv
Aray Karjauv

Reputation: 2945

Hier is my solution based on the Matheus Araujo's answer.

Let's say we have a country column. First, you must convert your column to categorical data type:

df.country = df.country.astype('category')

Get codes for each value as an array:

df.country.cat.codes

Convert the codes array back to strings

df.country.cat.categories[df.country.cat.codes]

You can also pass a list of integers

df.country.cat.categories[[0, 1, 2]]

Or a single code

df.country.cat.categories[0]

Upvotes: 14

Matheus Araujo
Matheus Araujo

Reputation: 5739

If you run this:

df["column_category"].cat.categories.get_loc("item")

It will return the code (e.g 0) that corresponds to the "item" in the mapping.

If you run this:

df["column_category"].cat.categories[0]

It will return the value of the code (e.g "item") that corresponds to the position 0 of the mapping

Upvotes: 5

JohnE
JohnE

Reputation: 30414

Method 1

You can create a dictionary mapping by enumerating (similar to creating a dictionary from a list by creating dictionary keys from the list indices):

dict( enumerate(df['x'].cat.categories ) )

# {0: 'bad', 1: 'good', 2: 'great'}

Method 2

Alternatively, you could map the values and codes in every row:

dict( zip( df['x'].cat.codes, df['x'] ) )

# {0: 'bad', 1: 'good', 2: 'great'}

It's a little more transparent what is happening here, and arguably safer for that reason. It is also much less efficient as the length of the arguments to zip() is len(df) whereas the length of df['x'].cat.categories is only the count of unique values and generally much shorter than len(df).

Additional Discussion

The reason Method 1 works is that the categories have type Index:

type( df['x'].cat.categories )

# pandas.core.indexes.base.Index

and in this case you look up values in an index just as you would a list.

There are a couple of ways to verify that Method 1 works. First, you can just check that a round trip retains the correct values:

(df['x'] == df['x'].cat.codes.map( dict( 
            enumerate(df['x'].cat.categories) ) ).astype('category')).all()
# True

or you can check that Method 1 and Method 2 give the same answer:

(dict( enumerate(df['x'].cat.categories ) ) == dict( zip( df['x'].cat.codes, df['x'] ) ))

# True

Upvotes: 93

Related Questions