I'm having some difficulties with a Pandas query and would appreciate some help. The DataFrame I'm working with is similar to the following:
data = {
"cusID": ["001", "002", "003", "004", "001", "003", "002", "001", "003", "002", "004"],
"productID": ["A", "A", "A", "A", "B", "B", "C", "D", "D", "E", "E"]
}
df = pd.DataFrame(data)
print(df)
cusID productID
0 001 A
1 002 A
2 003 A
3 004 A
4 001 B
5 003 B
6 002 C
7 001 D
8 003 D
9 002 E
10 004 E
I would like to get a new DataFrame of customers who have bought 2 or more of the same products. Given the above example, the output DataFrame I'm looking for is the following:
left_cusID right_cusID result
0 001 003 3
1 002 004 2
The result column is equal to the number of shared products between the two customers.
Also note that for this problem we assume that a customer can't purchase more than one of the same productID.
I thought of this problem for a while before posting it here but I couldn't find the appropiate steps/logic to solve it. At one point I thought I could cross-join the table with itself to have all combinations of left/right cusIDs but I got stuck quickly with that also. Any help would be appreciated.