I have a testing_df
organized like so:
# Use the arrays to create a dataframe
testing_df =pd.DataFrame(test_array,columns=['transaction_id','product_id'])
# Split the product_id's for the testing data
testing_df.set_index(['transaction_id'],inplace=True)
print(testing_df.head(n=5))
transaction_id product_id
001 (P01,)
002 (P01, P02)
003 (P01, P02, P09)
004 (P01, P03)
005 (P01, P03, P05)
I then performed some calculations on it and created a dictionary to store the results:
# Initialize a dictionary to store the matches
matches = {}
# Return the product combos values that are of the appropriate length and the strings match
for transaction_id,i in enumerate (testing_df['product_id']):
recommendation = None
recommended_count = 0
for k, count in product_combos.items():
k = list(k)
if len(i)+1 == len(k) and count >= recommended_count:
for product in i:
if product in k:
k.remove(product)
if len(k) == 1:
recommendation = k[0]
recommended_count = count
matches[transaction_id] = recommendation
print(matches)
[out]
{0: 'P09', 1: 'P09', 2: 'P06', 3: 'P09', 4: 'P09', 5: 'P09'}
The problem I have is that the keys of the matches dictionary should be 001,002,003,004,005 etc. - corresponding to the index of the test_df which is 001-100.
The second issue I have is that I would like to fill another dictionary (recommendations) with the keys being 001-100. I would like the fill the values from matches into this dict by matching the key-values. any help would be appreciated thank you!