Python_newbie
Python_newbie

Reputation: 43

Issue with matching keys of dictionary to index of a data frame

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!

Upvotes: 2

Views: 270

Answers (1)

quant_econ_geo
quant_econ_geo

Reputation: 136

So inside your for loop when using enumerate, your transaction_id will just be an integer. When you use that as the key in your dictionary it will show up as 1 and not 001. If you really want to work around that, you will have to convert it to a string, so instead of doing

matches[transaction_id]=recommendation

do

matches[str(transaction_id).zfill(3)]=recommendation

Or you could do a for loop over your index so something like

for ind in df.index

I'm not sure what you mean with your second question. To create an empty dictionary from your transaction ids just do

dict.fromkeys(list(df.index))

as pointed out here

Upvotes: 2

Related Questions