0

Hy I have a dataframe with the following structure

Id Vehicle1 Vehicle2
1 car motorcycle
2 bike car
3 motorcycle bike

And I want to group in one dataframe with onlyone column vehicle with that structure

Id Vehicle
1 car
1 motorcycle
2 bike
2 car
3 motorcycle
3 bike

how can I do it with python/pandas?

I tried with groupby but i failed.

Thanks!

1 Answers1

0
df.set_index("Id").stack()

This will sort all elements with the same Id by their respective column order.

BVB44
  • 266
  • 2
  • 11