1

If I have a df like this:

df1 = pd.DataFrame({'col1': [1, 1, 2, 2],
                   'col2': [10, 20, 10, 20]})

How would I get a list that pairs each row of col1 and col2 like so:

outputlist = [[1, 10], [1, 20], [2, 10], [2, 20]]

I've found ways to turn lists into df columns, but not the other way around!

Liquidity
  • 625
  • 6
  • 24

1 Answers1

2

You could do (see tolist):

import pandas as pd

df1 = pd.DataFrame({'col1': [1, 1, 2, 2],
                   'col2': [10, 20, 10, 20]})

result = df1.values.tolist()
print(result)

Output

[[1, 10], [1, 20], [2, 10], [2, 20]]
Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76