# How do I assign specific values to categorical variables

I have a Pandas data frame with columns within a survey with the following categorical values - "Increased, Decreased, Neutral". My question is how can I assign specific numerical values to these categorical values, namely +1 for Increased, -1 for Decreased and 0 for Neutral.

One possible way to map from string values to specific numerical values is by using a Python dictionary as a lookup table. The lookup table can be used for each value in the column with .apply(func) on the column.

import pandas as pd
l = [{'col1':'Increased'},{'col1':'Decreased'},{'col1':'Neutral'}]
df = pd.DataFrame(l)
print(df)


Output:

        col1
0  Increased
1  Decreased
2    Neutral


Create mapping and apply:

value_map_d = {'Increased':1,'Neutral':0,'Decreased':-1}
df['col1_numerical'] = df['col1'].apply(lambda x: value_map_d.get(x))
print(df)


Output:

        col1  col1_numerical
0  Increased               1
1  Decreased              -1
2    Neutral               0


In the example above, I read the values column col1 and write the numerical codes to a new column in the data frame called col1_numerical. The original column is dtype object and the output column is dtype int64 because all the values retrieved in the value map are integers.

Building on to @grov's answer, you can alternatively use map directly on the column like so:

df['col1_numerical'] = df['col1'].map({
"Increased": 1,
"Decreased": -1,
"Neutral": 0
})