I have a dataset containing two columns with frozensets. Now I would like to merge/take the union of these frozensets. I can do this with a for loop, however my dataset contains > 27 million rows, so I am looking for a way to avoid the for loop. Anyone any thoughts?
Data
import pandas as pd
import numpy as np
d = {'ID1': [frozenset(['a', 'b']), frozenset(['a','c']), frozenset(['c','d'])],
'ID2': [frozenset(['c', 'g']), frozenset(['i','f']), frozenset(['t','l'])]}
df = pd.DataFrame(data=d)
Code with for loop
from functools import reduce
df['frozenset']=0
for i in range(len(df)):
df['frozenset'].iloc[i] = reduce(frozenset.union, [df['ID1'][i],df['ID2'][i]])
Desired output
ID1 ID2 frozenset
0 (a, b) (c, g) (a, c, g, b)
1 (a, c) (f, i) (a, c, f, i)
2 (c, d) (t, l) (c, d, t, l)