Jamess11
Jamess11

Reputation: 174

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

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?

Upvotes: 1

Views: 500

Answers (2)

Fatemeh Sangin
Fatemeh Sangin

Reputation: 551

As simple as using dummy variables:

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

Upvotes: 2

mozway
mozway

Reputation: 262124

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

Upvotes: 2

Related Questions