Reputation: 1035
I am very new to python. I have a data frame that looks like this:
A B
E 0 1
F 2 3
I want to convert this data frame to a list that looks like this:
[[E, A, 0], [E, B, 1], [F, A, 2], [F, B, 3]]
Any idea?
Upvotes: 1
Views: 391
Reputation: 323236
df.reset_index().melt('index').values.tolist()
Out[1423]: [['E', 'A', 0], ['F', 'A', 2], ['E', 'B', 1], ['F', 'B', 3]]
Upvotes: 2
Reputation: 76917
Use
In [197]: df.stack().reset_index().values.tolist()
Out[197]: [['E', 'A', 0L], ['E', 'B', 1L], ['F', 'A', 2L], ['F', 'B', 3L]]
Upvotes: 2