0

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

Marie K
  • 1
  • 1
  • 1
  • 2
    How do you plan on encoding 'Movement'? If you merge those 3 into a single value, "Movement" will always have a value of 1. – NotAName Nov 11 '20 at 08:21
  • I would like to have "Movement" as a category with the three variables included. Sorry for the confusion! – Marie K Nov 11 '20 at 09:43

1 Answers1

1

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
StupidWolf
  • 45,075
  • 17
  • 40
  • 72
  • what if we have `df = pd.DataFrame({'CAR':[0,0,0,0,0,1],"BIKE":[1,1,0,1,0,1],"FEET":[0,0,1,0,1,1]})` then `df['Movement'] ` doesn't return back `CAR,BIKE,FEET` but It only returns the first one `CAR` . How we can fix this case? – Mario Nov 11 '20 at 10:43