Transform DataFrame containing ID pairs into a list of sets
22:33 28 Oct 2025

I have a Pandas DataFrame with the following structure

left_id right_id
a b
c a
x y

I need to transform this into a list of sets, like

[
  {'a', 'b', 'c'},
  {'x', 'y'}
]

the first two rows should be combined as a single set, because row 1 has id a and b and row 2 has ids c and a which, in this df, means the three IDs are related.

How can I do this?

python pandas dataframe