your use case makes sense to me, but it implies that your example data isn't suitable , and certainly your desired output is incorrect, as there are no examples of any AE for which 3 patients had the same pairings of drugs
look at AE 'm'.
filter(sample_df,AE=='m')
ID D AE
1 1 a m
2 2 e m
3 2 z m
4 3 b m
5 4 f m
6 5 b m
any drug, is unique to a single ID. apart from drug b which patients 3 and 5 had.
but these dont coincide with a , and so the output that would pair a with b for m is unjustified?
I could probably invent some data myself, but I don't want to waste my time if this is a misunderstanding