I have a dataframe describing for each transaction (TX_ID) which addresses have been used.
For simplicity, the dataframe could look like this:
TX_ID Address
1 "address2"
2 "address3"
2 "address1"
1 "address1"
2 "address4"
3 "address50"
I wish to identify clusters according to two simple rules:
- All addresses used in a transaction belong to the same cluster
c - Transitivity: if an address appears in multiple transactions, the transitivity property applies and all addresses in such transactions belong to the same cluster
For instance, according to the previous dataframe I would expect:
# CLUSTER1: ['address1', 'address2', 'address3', 'address4']
# CLUSTER2: ['address50']
I started by grouping on TX_ID to have all addresses used in each transaction:
df = df.groupby('tx_id')
TX_ID Address
1 "address1"
1 "address2"
2 "address1"
2 "address3"
2 "address4"
3 "address50"
After that I don't know how I could group them according to the transitivity rule without iterating over the groups and doing some checks. Is there a easier way? Thanks in advance.
