cartesian product in pandas

前端 未结 11 1819
再見小時候
再見小時候 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:59

    Minimal code needed for this one. Create a common 'key' to cartesian merge the two:

    df1['key'] = 0
    df2['key'] = 0
    
    df_cartesian = df1.merge(df2, how='outer')
    

提交回复
热议问题