lkp = pd.Series(memberships).apply(set)
n, m = congruent.shape
c = congruent.columns.to_series().map(lkp).values
r = congruent.index.to_series().map(lkp).values
Vectorized Lookups of Pandas Series to a Dictionary