I have a table on the following format
Id | Sequence | Attribute A | Attribute B |
ID1 [A,B,C,D] A1 B1
ID2 [A,B,F,G] A2 B3
ID3 [A,B,C,D] A1 B1
I want to calculate, for each event combination and attribute value, count the number of unique ID's.
The final table should look like
Pair | Attribute Type | Attribute Value | ID Count
(A,B) Attribute A A1 2 #Event A happens before event B in 2 unique ID's where A1 is the value of Attribute A.
(A,C) Attribute A A1 2
(A,D) Attribute A A1 2
(B,C) Attribute A A1 2
(B,D) Attribute A A1 2
(C,D) Attribute A A1 2
(A,B) Attribute A A2 1
(A,F) Attribute A A2 1
(A,G) Attribute A A2 1
(B,F) Attribute A A2 1
(B,G) Attribute A A2 1
(F,G) Attribute A A2 1
(A,B) Attribute B B1 2
(A,C) Attribute B B1 2
(A,D) Attribute B B1 2
(B,C) Attribute B B1 2
(B,D) Attribute B B1 2
(C,D) Attribute B B1 2
(A,B) Attribute B B3 1
(A,F) Attribute B B3 1
(A,G) Attribute B B3 1
(B,F) Attribute B B3 1
(B,G) Attribute B B3 1
(F,G) Attribute B B3 1
What would be the correct way of doing this? In reality I will have more than just 2 Attributes.
This is how far I have come
df['Sequence Combs'] = df['Sequence'].apply(lambda x: list(itertools.combinations(x,2)))
Id | Sequence | Event Combs | Attribute A | Attribute B |
ID1 [A,B,C,D] [(A,B),(A,C),(A,D),(B,C),(B,D),(C,D)] A1 B1
ID2 [A,B,F,G] [(A,B),(A,F),(A,G),(B,F),(B,G),(F,G)] A2 B3
ID3 [A,B,C,D] [(A,B),(A,C),(A,D),(B,C),(B,D),(C,D)] A1 B1
And after doing explode
df = df.explode('Sequence Combs')
I get the following
Id | Sequence | Event Combs | Attribute A | Attribute B |
ID1 [A,B,C,D] (A,B) A1 B1
ID1 [A,B,C,D] (A,C) A1 B1
ID1 [A,B,C,D] (A,D) A1 B1
ID1 [A,B,C,D] (B,C) A1 B1
ID1 [A,B,C,D] (B,D) A1 B1
ID1 [A,B,C,D] (C,D) A1 B1
... ... .. .. ..
But I am unsure on how to proceed from here, any ideas?