Priya Chauhan
Priya Chauhan

Reputation: 485

Convert Categorical values to custom number in pandas dataframe

I have a pandas dataframe, in which I have categorical values along with numerical ones. Now instead of getting binary data for categorical data, I want to customize it.

Suppose if I have values like high and low. pandas is giving low = 1, and high = 0, which I don't want.

For Example :

df_bin=pd.get_dummies(global_df[['CATEGORY','IMPACT']])

now event category has values past and the above operation gives past as 1 not 0

so, how can I make it 0

Another column is area having values sales, advertisement Now for these values, I want to give a custom number

Upvotes: 1

Views: 2241

Answers (1)

JayPeerachai
JayPeerachai

Reputation: 3842

new_label = {"cat_column": {"low": 1, "high": 0}}
df.replace(new_label , inplace = True)

To do custom label encoding, create the dict of mappings and use replace() to replace your categorical values with numerical ones. You can vary your numerical value depending on your preference.

Hope this is what you are looking for.

Upvotes: 2

Related Questions