0

I have this dataframe:

Category 3,Chapter I,Chapter II,Chapter III
prima,126,97,83
da,38,24,24
sps.,22,17,18

How to make it like:

prima,126,Chapter I
prima, 97, Chapter II
prima, 83, Chapter III
da, 38, Chapter I
[...]
Anna
  • 369
  • 2
  • 10

1 Answers1

0

You can run:

result = df.set_index('Category 3').stack().rename('Value').reset_index()

For your data sample the result is:

  Category 3      level_1  Value
0      prima    Chapter I    126
1      prima   Chapter II     97
2      prima  Chapter III     83
3         da    Chapter I     38
4         da   Chapter II     24
5         da  Chapter III     24
6       sps.    Chapter I     22
7       sps.   Chapter II     17
8       sps.  Chapter III     18

If you want, rename level_1 column to any meaningful name and change the order of columns.

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41