Let's say I have a dataframe df1:
df1_unit base_unit
0 x x
1 y x
2 z z
3 t z
4 u z
and another called df2:
df2_unit base_unit
0 a b
1 b b
2 c c
3 d e
4 e e
and yet another dataframe df_eq that gives the equivalence between groups:
df1_unit df2_unit
0 x b
1 z e
In case of df1, the base_unit is basically the df1_unit that acts as a parent unit for all the units in a group. i.e. x is the base unit with which the x, y, z units are identified as a group. Similarly for df2.
I'm trying to generate a dataframe with all possible pairs of items from equivalent groups, but without the pairs in the df_eq dataframe (this is a restriction we impose). In this case, the unrestricted output would be:
df1_unit df2_unit
0 x a
1 x b (shouldn't be included)
2 y a
3 y b
4 z d
5 z e (shouldn't be included)
6 t d
7 t e
8 u d
9 u e
and the desired, restricted output would be:
df1_unit df2_unit
0 x a
1 y a
2 y b
3 z d
4 t d
5 t e
6 u d
7 u e
I'm having difficulty in generating even the unrestricted output without resorting to ridiculous brute force methods. Is there an efficient way to achieve the desired output?
EDIT: I've made some headway using the following code:
temp = dfeq.rename(columns={'df2u':'base_unit'}).merge(df2, on='base_unit', how='left')
temp = temp[['df1u', 'df2u']]
out = temp.rename(columns={'df1u':'base_unit'}).merge(df1, on='base_unit', how='left')
out = out[['df1u', 'df2u']]
Does this seem correct? Also, I'm not sure about how to remove the rows in out that are also present in dfeq.