cartesian product in pandas

前端 未结 11 1823
再見小時候
再見小時候 2020-11-21 23:35

I have two pandas dataframes:

from pandas import DataFrame
df1 = DataFrame({\'col1\':[1,2],\'col2\':[3,4]})
df2 = DataFrame({\'col3\':[5,6]})     

11条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-21 23:56

    If you have no overlapping columns, don't want to add one, and the indices of the data frames can be discarded, this may be easier:

    df1.index[:] = df2.index[:] = 0
    df_cartesian = df1.join(df2, how='outer')
    df_cartesian.index[:] = range(len(df_cartesian))
    

提交回复
热议问题