0

I've a sample dataframe

s_id      c1_id     c2_id       c3_id
  1        a          b           c
  2        a          b
  3        x          y           z

how can I transpose the dataframe to

s_id    c_id
 1        a
 1        b
 1        c
 2        a
 2        b
 3        x
 3        y
 3        z

1 Answers1

0

Here you go

df.set_index("s_id").stack().droplevel(1)

Result:

s_id
1    a
1    b
1    c
2    a
2    b
3    x
3    y
3    z
dtype: object

Explanation:

  1. Set s_id as index
  2. Apply stack so every column is stack on each other.
  3. We remove names stacked columns, because we don't need them.
Aidis
  • 1,272
  • 4
  • 14
  • 31