Home > Enterprise >  Implement popular product recommendation system using pandas
Implement popular product recommendation system using pandas

Time:09-12

I have two data frame as shown below

df1:

cust_id           product_list
1                 ['phone', 'tv']
2                 ['ball', 'bat']
3                 ['bat']
4                 ['ball', 'bat', 'phone', 'tv']
5                 ['tv']
6                 ['bat']
7                 ['ball', 'bat', 'phone', 'tv']       
8                 ['phone', 'tv']

df2:

product          support
ball             0.18
bat              0.29
phone            0.24
tv               0.29   

From the above I would like to prepare following data frame.

Expected output:

    cust_id      product_list             recommended_dictionary
0   1            [phone, tv]              {'bat': 0.29, 'ball': 0.18}
1   2            [ball, bat]              {'tv': 0.29, 'phone': 0.24}
2   3            [bat]                    {'tv': 0.29, 'phone': 0.24, 'ball': 0.18}
3   4            [ball, bat, phone, tv]   {}
4   5            [tv]                     {'bat': 0.29, 'phone': 0.24, 'ball': 0.18}
5   6            [bat]                    {'tv': 0.29, 'phone': 0.24, 'ball': 0.18}
6   7            [ball, bat, phone, tv]   {}
7   8            [phone, tv]              {'bat': 0.29, 'ball': 0.18}

I tried below code and it worked well, but I would like to know is there any better way to do the same.

Create a dictionary with popularity

popularity_dict = dict(df2.values)
        

Function to recommend the product in the order of popularity if a customer does not have that product

def f(x):
    out = {}
    dif = [i for i in popularity_dict.keys() if i not in x]
    for i in dif:
        out[i] = popularity_dict[i]
        
    return out

Compute the recommendation dicrionary in the order of popularity if the customer does not have that product

df1['popularity_based_recommended_dictionary'] = df1['product_list'].apply(f)

CodePudding user response:

You can sorting sdescendig ouput dictioanry, also simplify your code:

def f(x):
    out = {k: v for k, v in popularity_dict.items() if k not in x}
    return dict(sorted(out.items(), reverse=True))

df1['popularity_based_recommended_dictionary'] = df1['product_list'].apply(f)
print (df1)
   cust_id                    product_list  \
0        1                 ['phone', 'tv']   
1        2                 ['ball', 'bat']   
2        3                         ['bat']   
3        4  ['ball', 'bat', 'phone', 'tv']   
4        5                          ['tv']   
5        6                         ['bat']   
6        7  ['ball', 'bat', 'phone', 'tv']   
7        8                 ['phone', 'tv']   

      popularity_based_recommended_dictionary  
0                 {'bat': 0.29, 'ball': 0.18}  
1                 {'tv': 0.29, 'phone': 0.24}  
2   {'tv': 0.29, 'phone': 0.24, 'ball': 0.18}  
3                                          {}  
4  {'phone': 0.24, 'bat': 0.29, 'ball': 0.18}  
5   {'tv': 0.29, 'phone': 0.24, 'ball': 0.18}  
6                                          {}  
7                 {'bat': 0.29, 'ball': 0.18}  
  • Related