0

GOAL: To turn my results that are in list format into a data frame that I can print. I have an index error, I tried to change the index numbers...but I don't think that is the underlying issue. Assistance would be much appreciated.

print(len(results)) #of lists

1072

print(results[0]) #First case to use for format 

RelationRecord(items=frozenset({'Instant food products', 'hamburger meat'}), 
support=0.003050330452465684, ordered_statistics=[OrderedStatistic(items_base=frozenset({'Instant 
food products'}), items_add=frozenset({'hamburger meat'}), confidence=0.379746835443038, 
lift=11.42143769597027)])

MY CODE:

results_w = []
for item in results:
pair = item[0]
items = [x for x in pair]

value0 = str(items[0])
value1 = str(items[1])

value2 = str(item[1])[:7]

value3 = str(item[2][0][2])[:7]
value4 = str(item[2][0][3])[:7]

rows = (value0, value1, value2, value3, value4)
results.append(rows)

lables = ['Item1','Item2','Support','Confidence','Lift']
Grocery_Pairings = pd.DataFrame.from_records(results, columns =lables)
print(Grocery_Pairings)

ERROR MESSAGE:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)

<ipython-input-34-9daa56933611> in <module>

      9     value2 = str(item[1])[:7]
     10 
---> 11     value3 = str(item[2][0][2])[:7]
     12     value4 = str(item[2][0][3])[:7]
     13 

IndexError: string index out of range

EXPECTED RESULT:

Item1                 Item2          Support Confidence Lift
instant food products hamburger meat 0.00305 0.37974    11.4214
  • Seems like there should be a better way of accessing items then just indexing multiple levels. What package(s) are you using that generates a `RelationRecord` object? – G. Anderson Jul 16 '20 at 18:18
  • apyori 1.1.2, I'm doing a relationship analysis. The data has to be analyzed in list form, it is returned the same way. – Amber Williams Jul 16 '20 at 18:30
  • Unfortunately because we can't reproduce your situation to make a [mcve], it could be difficult to diagnose. My recommendation would be this: Given that your error is telling you that one of your indices does not exist, split the offending line into more than one line and check each step. Like `val_level1=item[2];val_level2=val_level1[0];val_level3=val_level2[2]` then check the contents of `val_level3` to see if the output is what you expect (and repeat with the higher levels if it isn't) – G. Anderson Jul 16 '20 at 18:39
  • I did, it runs until i'm assigning value3, where there is an indexing issue on an existing list. The list is what I provided...so maybe someone else will have more experience with this transformation. The code is saying for the second string in the list return these values, but I must be missing something nuanced. – Amber Williams Jul 16 '20 at 19:01

0 Answers0