0

I want to pass only a few rows and make the one hot encoding transformation of the categorical column into binary columns while keeping other columns (all unique values) with 0 and also keeping the NUMBER column and adding a prefix.

Example: giving the following dataframe

   TYPE NUMBER
0   A    1
1   B    2
2   C    3

the transformation into binary is:

df = pd.DataFrame({'TYPE': ['A', 'B', 'C'],
                   'NUMBER': [1, 2, 3]})
 
print(pd.get_dummies(df, prefix=['type'],prefix_sep="_",columns=['TYPE']))

I want to achieve the following result:

send for example this dataframe

and get (even if other values like A are not mentioned keep the column with 0 and keep also NUMBER column and the prefix "type"):

    NUMBER    type_A    type_B    type_C
0       6         0         0         1
1       7         0         1         0
2       9         0         1         0

How can I achieve this using Python?

Legna
  • 125
  • 1
  • 1
  • 10

0 Answers0