# Getting count of frequent itemsets in Python mlxtend

I have a dataset:

[['Apple', 'Beer', 'Chicken', 'Rice'],
['Apple', 'Beer', 'Rice'],
['Apple', 'Beer'],
['Apple', 'Bananas']]


I am using the frequent itemsets tools, specifically apriori, to find frequent datasets. I convert to a OneHotDataset, then to a Data Frame:

oht = OnehotTransactions()
oht_ary = oht.fit(tempList).transform(tempList)
oht_ary

df = pd.DataFrame(oht_ary, columns=oht.columns_)
df


Then I use the Apriori function:

frequent_itemsets = apriori(df, min_support=0.01, use_colnames=True)


I see this table, which is what I expect:

support                      itemsets
0      1.00                       [Apple]
1      0.25                     [Bananas]
2      0.75                        [Beer]
3      0.25                     [Chicken]
4      0.50                        [Rice]
5      0.25              [Apple, Bananas]
6      0.75                 [Apple, Beer]
7      0.25              [Apple, Chicken]
8      0.50                 [Apple, Rice]
9      0.25               [Beer, Chicken]
10     0.50                  [Beer, Rice]
11     0.25               [Chicken, Rice]
12     0.25        [Apple, Beer, Chicken]
13     0.50           [Apple, Beer, Rice]
14     0.25        [Apple, Chicken, Rice]
15     0.25         [Beer, Chicken, Rice]
16     0.25  [Apple, Beer, Chicken, Rice]


The question I have is, is there any way to print a suppport "count" of occurrences of the datasets-meaning how many times that itemset appears in the transactions? One thing I messed around with from the mlxtend site was this being added to add an additional column of length but couldn't get a count piece:

frequent_itemsets['length'] = frequent_itemsets['itemsets'].apply(lambda x: len(x))


EDIT: Ok I think I am confusing folks. What I would like to see is:

      support     itemsets    count
0      1.00       [Apple]       4
1      0.25      [Bananas]      1
2      0.75         [Beer]      3
3      0.25       [Chicken]     1


I guess I could just create a column that uses the total support and multiplies by the relative support, but was looking for a more systematic way to do it.

• Do you want to count how often an item set occurs in the dataset? That would be from collections import Counter Counter(tempList) Feb 15, 2018 at 17:20
• I tried to use this but got "List is not hashable" Feb 15, 2018 at 18:14
• Make it a tuple and then use the Counter Feb 15, 2018 at 18:21
• The problem is that lists are mutable. That's why you need to cast them to tuples.
– Emre
Feb 15, 2018 at 18:59