1

I did one hot encoding on my categorical variables in my dataframe and my columns were renamed as per below..

before one hot encoding

d = {'PROD_ID': ['OM', 'RM', 'VL']
df = pd.DataFrame(data=d)
full_data = pd.get_dummies(data, drop_first=True)


After one hot encoding

full_data

PROD_ID_b'OM' 
PROD_ID_b'VL'
PROD_ID_b'RM'

I need to remove b and '' from above dataframe, i.e i need PROD_ID_OM 
                                                           PROD_ID_VL  
                                                           PROD_ID_RM
sophros
  • 14,672
  • 11
  • 46
  • 75
Tumi Sebela
  • 51
  • 1
  • 6
  • Does this answer your question? [Reverse a get\_dummies encoding in pandas](https://stackoverflow.com/questions/50607740/reverse-a-get-dummies-encoding-in-pandas) – sophros Feb 13 '20 at 06:40

1 Answers1

0

You can pass the prefix param to get_dummies method as follows, then it will add the prefix to all columns as you need.

df = pd.DataFrame({'PROD_ID': ['OM', 'RM', 'VL']})
nwdf = pd.get_dummies(df,prefix=['PROD_ID'])
print(nwdf.columns)

Output: Index(['PROD_ID_OM', 'PROD_ID_RM', 'PROD_ID_VL'], dtype='object')

enter image description here

Dinith Minura
  • 1,446
  • 2
  • 12
  • 24