0

If I have pandas data frame like this:

  | F1 | F2 | F3 | Flag
  ---------------------
0 | 10 | 22 | 54 | True
1 | 3  | 77 | 9  | False

How can I turn it into this:

  | F  | Val | Flag
  -------------------
0 | F1 | 10  | True
1 | F2 | 22  | True
2 | F3 | 54  | True
3 | F1 | 3   | False
4 | F2 | 77  | False
5 | F3 | 9   | False

I've looked into pivot tables and multi indexing, but i'm having a hard time quite getting it to fit together.

Mayank Porwal
  • 31,737
  • 7
  • 30
  • 50
Warlax56
  • 971
  • 3
  • 22

1 Answers1

2

Use df.melt:

In [141]: df.melt(id_vars='Flag', var_name='F', value_name='Val')
Out[141]: 
    Flag   F  Val
0   True  F1   10
1  False  F1    3
2   True  F2   22
3  False  F2   77
4   True  F3   54
5  False  F3    9
Mayank Porwal
  • 31,737
  • 7
  • 30
  • 50