I'm not sure the wording of the title is optimal, because the problem I have is a little tricky to explain. In code, I have a df that looks something like this:

```
import pandas as pd
import numpy as np 
a = ['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'D', 'E', 'E']
b = [3, 1, 2, 3, 12, 4, 7, 8, 3, 10, 12]
df = pd.DataFrame([a, b]).T

df

```

Yields

```
    0   1
0   A   3
1   A   1
2   A   2
3   B   3
4   B  12
5   B   4
6   C   7
7   C   8
8   D   3
9   E  10
10  E  12

```

I'm aware of groupby methods to group by values in a column, but that's not exactly what I want. I kind of want to go a step past that, where any intersection in column 1 between groups of column 0 are grouped together. My wording is terrible (which is probably why I'm having trouble putting this into code), but here's basically what I want as output:

```
    0   1
0   A-B-D-E   3
1   A-B-D-E   1
2   A-B-D-E   2
3   A-B-D-E   3
4   A-B-D-E  12
5   A-B-D-E   4
6   C   7
7   C   8
8   A-B-D-E   3
9   A-B-D-E  10
10  A-B-D-E  12

```

Basically, A, B, and D all share the value 3 in column 1, so their labels get grouped together in column 0. Now, because B and E share value 12 in column 1, and B shares the value 3 in column 1 with A and D, E gets grouped in with A, B, and D as well. The only value in column 0 that remained independent is C, because it has no intersections with any other group.

In my head this ends up being a recursive loop, but I can't seem to figure out the exact logic. Any help would be appreciated.