cartesian product in pandas

前端 未结 11 1791
再見小時候
再見小時候 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-22 00:15

    This won't win a code golf competition, and borrows from the previous answers - but clearly shows how the key is added, and how the join works. This creates 2 new data frames from lists, then adds the key to do the cartesian product on.

    My use case was that I needed a list of all store IDs on for each week in my list. So, I created a list of all the weeks I wanted to have, then a list of all the store IDs I wanted to map them against.

    The merge I chose left, but would be semantically the same as inner in this setup. You can see this in the documentation on merging, which states it does a Cartesian product if key combination appears more than once in both tables - which is what we set up.

    days = pd.DataFrame({'date':list_of_days})
    stores = pd.DataFrame({'store_id':list_of_stores})
    stores['key'] = 0
    days['key'] = 0
    days_and_stores = days.merge(stores, how='left', on = 'key')
    days_and_stores.drop('key',1, inplace=True)
    

提交回复
热议问题