0

I am trying to restructure a pandas data frame. Let's say, I have the below data frame;

uri ID1 ID2 ID3
aaa 1 2 3
bbb 4 5 6

I am trying to restructure as below;

uri IDs
aaa 1
aaa 2
aaa 3
bbb 4
bbb 5
bbb 6

I know the transform function but it is not doing what I want. Can anyone please help me?

1 Answers1

0

Use stack:

>>> df.set_index('uri').stack().droplevel(1).rename('IDs').reset_index()
   uri  IDs
0  aaa    1
1  aaa    2
2  aaa    3
3  bbb    4
4  bbb    5
5  bbb    6
Corralien
  • 109,409
  • 8
  • 28
  • 52