Home > Software design >  Overlay pd.DataFrame rows to a list of lists per column
Overlay pd.DataFrame rows to a list of lists per column

Time:07-07

Let's say that I have two pandas DataFrames

df1 = pd.DataFrame({'a': [1], 'b': [2], 'c': [3]})
df2 = pd.DataFrame({'a': [4], 'b': [5], 'c': [6]})

I would like to obtain a list of lists which hold the values per column, looking as follows

> [[1, 4], [2, 5], [3, 6]]

How could that be done without iterating over the columns?

I appreciate any help. Thanks in advance!

CodePudding user response:

You can use zip.

>>> list(zip(*df1.values, *df2.values))
[(1, 4), (2, 5), (3, 6)]

>>> list(map(list, zip(*df1.values, *df2.values)))
[[1, 4], [2, 5], [3, 6]]
  • Related