Home > Blockchain >  How to pivot one colum with n categories into n binary values column?
How to pivot one colum with n categories into n binary values column?

Time:12-07

I have a following dataframe:

id gender name ... status
1 M John ... Withdrawn
2 F Mary ... Pass
... ... ... ... ...
10 F Kate ... Fail

And I want to transform it into a dataframe like this:

id gender name ... Withdrawn Pass Fail
1 M John ... 1 0 0
2 F Mary ... 0 1 0
... ... ... ... ... ... ...
10 F Kate ... 0 0 1

Is something like this possible with using functions like pivot_table or is it necessary to write a function and then loop through every row and append a value to corresponding column?

CodePudding user response:

As simple as using dummy variables:

df = pd.get_dummies(df, columns=['status'])
df = df.drop(columns = ['status'])

CodePudding user response:

Use pandas.get_dummies and join on the original dataframe in which you dropped the 'status' column:

df.drop(columns='status').join(pd.get_dummies(df['status']))

output:

   id  gender  name    Fail  Pass  Withdrawn
0    1      M   John      0     0          1
1    2      F   Mary      0     1          0
2   10      F   Kate      1     0          0
  • Related