1

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?

2 Answers 2

2

Use

In [197]: df.stack().reset_index().values.tolist()
Out[197]: [['E', 'A', 0L], ['E', 'B', 1L], ['F', 'A', 2L], ['F', 'B', 3L]]
Sign up to request clarification or add additional context in comments.

1 Comment

You win for 3s :-)
2
df.reset_index().melt('index').values.tolist()
Out[1423]: [['E', 'A', 0], ['F', 'A', 2], ['E', 'B', 1], ['F', 'B', 3]]

1 Comment

@BahadorSaket Go ahead :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.