Get all combinations of elements from two lists?

后端 未结 3 1992
滥情空心
滥情空心 2020-11-29 09:52

If I have two lists

l1 = [ \'A\', \'B\' ]

l2 = [ 1, 2 ]

what is the most elegant way to get a pandas data frame which looks like:

3条回答
  •  自闭症患者
    2020-11-29 10:51

    use product from itertools:

    >>> from itertools import product
    >>> pd.DataFrame(list(product(l1, l2)), columns=['l1', 'l2'])
      l1  l2
    0  A   1
    1  A   2
    2  B   1
    3  B   2
    

提交回复
热议问题