Search code examples
python-3.xpandaslistapriorifrozenset

Frozenset to list yields wrong results


Error image

What I did is:

a = dataframe.antecedants
print(type(a[0]))
print(a[10])
b = a.tolist()
print(type(b[10]))
print(b[10])
c = [list(x) for x in a]
print(type(c[10]))
print(c[10])

I was trying to save my apriori dataframe to Elasticsearch, as this contains frozenset so I was getting some error, so went converting frozenset to list, and here when I convert my frozenset to list I get wrong results. Why am I getting so? I just want to convert a frozenset column to list of lists.

The forzenset data is like:

frozenset image

Sample:

0                 (1)
1               (522)
4               (349)
5                (37)
6               (372)
7                (37)
8               (373)
9                (37)
10              (372)
11              (349)
12              (373)
13              (349)
14              (372)
15              (373)
16         (372, 349)
17          (372, 37)
18          (37, 349)
19              (372)
20              (349)
21               (37)
22         (349, 373)
23          (37, 373)

And libraries I am using are:

import pandas as pd
import numpy as np
from pandas.io.json import json_normalize
from mlxtend.frequent_patterns import apriori
from mlxtend.frequent_patterns import association_rules
from elasticsearch import Elasticsearch
import json

Then:

dataframe = apriori(dataframe, min_support=0.1, use_colnames=True)
dataframe = association_rules(dataframe, metric="lift", min_threshold=1)
new = dataframe.copy()

Basically convert the frozenset columns to list of lists is what I am trying to achieve.

Updated

Though When I did:

my_list = []
for antecedant in new.antecedants:
    my_list.append(list(antecedant))
my_list

this worked! but:

column_values = pd.Series(my_list)
new.insert(loc=0, column='new_column', value=column_values)
new

but again gave wrong results in dataframe.


Solution

  • new.reset_index(drop=True, inplace=True)
    

    Worked for me! as You see the indexes are not continuouse after apriori and association rules formation, so resetting index helped me out!