Search code examples
pythonpandasdictionarydataframecategorical-data

Convert Categorical codes to Categorical values


I have a dataframe:

ga_deviceCategory_codes ga_channelgrouping_codes    ga_sourceMedium_codes   
        1.0                         6.0                      9.0
        1.0                         6.0                      9.0    

Which I have converted into categorical codes from categorical values using :

data['ga_deviceCategory_codes'] = data['ga_deviceCategory'].astype('category').cat.codes
data['ga_channelgrouping_codes'] = data['ga_channelgrouping'].astype('category').cat.codes
data['ga_sourceMedium_codes'] = data['ga_sourceMedium'].astype('category').cat.codes

How do I get back to the original categorical values now from the above codes?


Solution

  • Category mappings are stored internally by Pandas, but not as a regular Python dictionary. You can create such a dictionary yourself to map backwards:

    df['mycol'] = df['mycol'].astype('category')
    d = dict(enumerate(df['mycol'].cat.categories))
    

    Then map backwards:

    df['mycol_codes'] = df['mycol'].cat.codes
    df['mycol_reversed'] = df['mycol_codes'].map(d)
    

    Be careful with this method. Make sure you create the dictionary straight after you convert to categories. When concatenating dataframes with categorical series, you may find the mapping changes.