Performant cartesian product (CROSS JOIN) with pandas

后端 未结 3 1309
一向
一向 2020-11-22 04:23

The contents of this post were originally meant to be a part of Pandas Merging 101, but due to the nature and size of the content required to fully do j

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 04:39

    Using itertools product and recreate the value in dataframe

    import itertools
    l=list(itertools.product(left.values.tolist(),right.values.tolist()))
    pd.DataFrame(list(map(lambda x : sum(x,[]),l)))
       0  1  2   3
    0  A  1  X  20
    1  A  1  Y  30
    2  A  1  Z  50
    3  B  2  X  20
    4  B  2  Y  30
    5  B  2  Z  50
    6  C  3  X  20
    7  C  3  Y  30
    8  C  3  Z  50
    

提交回复
热议问题