Get all combinations of elements from two lists?

后端 未结 3 1993
滥情空心
滥情空心 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:50

    You can also use the sklearn library, which uses a NumPy-based approach:

    from sklearn.utils.extmath import cartesian
    
    df = pd.DataFrame(cartesian((L1, L2)))
    

    For more verbose but possibly more efficient variants see Numpy: cartesian product of x and y array points into single array of 2D points.

提交回复
热议问题