0

DUPLICATE

Suppose I have two lists with three distinct values:

list1 = [1,2,3]
list2 = [10,11,12]

I want to create a dataframe which has a row for each possible combination of the two lists. In other words I want a dataframe which looks like this: (Where list1 and list2 are columns)

df = 

list1  list2
  1     10
  1     11
  1     12
  2     10
  2     11
  2     12
  3     10
  3     11
  3     12

I am getting stuck trying to figure out what the best way would be to do this. Note the sizes of the list are larger, I only use so few values as example.

2 Answers2

0

Use:

from  itertools import product
df = pd.DataFrame(product(list1, list2), columns=['list1','list2'])
print (df)
   list1  list2
0      1     10
1      1     11
2      1     12
3      2     10
4      2     11
5      2     12
6      3     10
7      3     11
8      3     12
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
0

This should work:

 pd.DataFrame(list(zip(list1, list2)),
      columns=['lst1_title','lst2_title'])
user1000x
  • 40
  • 8