In another topic, there is this problem I am trying to solve. There is a very detailed answer which for some reason does not work for me and gives
TypeError: 'list' object is not callable
df=
index sale_id item
33337606 02234563 389699
29350189 02234520 230153
5002610 02234403 P79927
3357151 02235866 I25240
29351311 02234520 230155
... ... ...
grp = df.groupby('sale_id')['item'].agg(lambda x: ''.join(x))
purchases = grp.apply(lambda x: ''.join(x)).unique()
unique_items = df.item.unique()
res = {}
for c in combinations(unique_items, 2):
c = set(c)
res[frozenset(c)] = 0
for i in purchases:
if c.intersection(i) == c:
res[frozenset(c)] = 1
for k, v in res.items():
res[k] = v / purchases.shape[0]
res
CodePudding user response:
My bad completely, sorry guys. I had not imported combinations.
from itertools import combinations