Pandas: How to pivot rows into columns

I am looking for a way to pivot around 600 columns into rows. Here’s a sample with only 4 of those columns (good, bad, ok, Horrible):

df:

RecordID   good   bad   ok  Horrible
  A          0      0    1     0 
  B          1      0    0     1

desired output:

 RecordID     Column    Value
   A           Good       0
   A            Bad       0
   A            Ok        1
   A          Horrible    0
   B           Good       1
   B            Bad       0
   B            Ok        0
   B          Horrible    1

Answer

You can use melt function:

(df.melt(id_vars='RecordID', var_name='Column', value_name='Value')
   .sort_values('RecordID')
   .reset_index(drop=True)
)

Output:

    RecordID      Column    Value
0          A        good        0
1          A         bad        0
2          A          ok        1
3          A    Horrible        0
4          B        good        1
5          B         bad        0
6          B          ok        0
7          B    Horrible        1