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?
>Solution :
As simple as using dummy variables:
df = pd.get_dummies(df, columns=['status'])
df = df.drop(columns = ['status'])