I have two dataframes representing products two distributors sell. They look like this:
df1 for distributor 1.
Country, Age group, Product
{USA, Canada}, {Toddlers, Teens}, {Jeans, Shoes}
{USA, Mexico}, {Teens, Adults}, {Hair bands, Mufflers}
So, the distributor 1 sells jeans and shoes for toddlers and teens in USA and Canada. They also sell hair bands and mufflers for teens and adults in USA and Mexico.
df2 for distributor 2.
Country, Age group, Product
{Russia, Canada}, {Toddlers, Babys}, {Shoes, Hats}
{Russia, USA}, {Adults, Elderly}, {Mufflers, Hats}
I want to find out if any particular value of Country, Age group and Product matches both df2 and df1. That is, I want of find out if there are conflicts where both distributor is trying to sell the same product to the same age group in the same country.
A straightforward approach would be to explode both the dataframes to look like this:
df1:
Country Age group Product
Canada Teens Jeans
Canada Teens Shoes
Canada Toddlers Jeans
Canada Toddlers Shoe
...
df2:
Country Age group Product
Canada Babys Hats
Canada Babys Shoes
Canada Toddlers Hats
Canada Toddlers Shoes
Russia Adults Hats
...
Then, convert them to to set of tuples, df1:
{('Canada', 'Teens', 'Jeans'),
('Canada', 'Teens', 'Shoes'),
('Canada', 'Toddlers', 'Jeans'),
...}
df2:
{('Canada', 'Babys', 'Hats'),
('Canada', 'Babys', 'Shoes'),
('Canada', 'Toddlers', 'Hats'),
('Canada', 'Toddlers', 'Shoes'),
('Russia', 'Adults', 'Hats'),
...}
Then find their intersection:
>>df1s.intersection(df2s)
{('Canada', 'Toddlers', 'Shoes'), ('USA', 'Adults', 'Mufflers')}
The problem I have with this approach is due to the number of columns and possible values in the set, the exploded dataframes have too many rows and final sets have too many items to fit in memory. Is there an (ideally efficient) algorithm to get this done without having to explode the dataframes?