Marie K
Marie K

Reputation: 1

Creating categorical variable of dummies in Python

I am trying to create a categorical variable out of three columns containing only 0 and 1. They would perfectly match being together a categorical variable - however I cannot find a code merging them to be one: being a categorical variable labeled "Movement"

    CAR BIKE FEET
0   0.0 1.0 0.0
1   0.0 1.0 0.0
2   0.0 0.0 1.0
3   0.0 1.0 0.0
4   0.0 0.0 1.0
... ... ... ...

Any thoughts or other ways to create the categorical variables would be appreciated.

EDIT: "Movement" should be an object having the three categorical variables included

Upvotes: 0

Views: 74

Answers (1)

StupidWolf
StupidWolf

Reputation: 46888

If I get you correct, you can use idxmax with axis=1 for rows :

df = pd.DataFrame({'CAR':[0,0,0,0,0],"BIKE":[1,1,0,1,0],"FEET":[0,0,1,0,1]})
df['Movement'] = df.idxmax(axis=1)
df

CAR BIKE    FEET    Movement
0   0   1   0   BIKE
1   0   1   0   BIKE
2   0   0   1   FEET
3   0   1   0   BIKE
4   0   0   1   FEET

Upvotes: 1

Related Questions