caparol6991

calc_recommendation

Dec 9th, 2019
202
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. def calc_recommendation(df_expl, df_pop):
  3. """Calculate recommendations based on popularity of items.
  4.  
  5. The final data frame will have an impression list sorted according to the number of clicks per item in a reference data frame.
  6.  
  7. :param df_expl: Data frame with exploded impression list
  8. :param df_pop: Data frame with items and number of clicks
  9. :return: Data frame with sorted impression list according to popularity in df_pop
  10. """
  11.  
  12. df_expl_clicks = (
  13. df_expl[GR_COLS + ["impressions"]]
  14. .merge(df_pop,
  15. left_on="impressions",
  16. right_on="reference",
  17. how="left")
  18. )
  19.  
  20. df_out = (
  21. df_expl_clicks
  22. .assign(impressions=lambda x: x["impressions"].apply(str))
  23. .sort_values(["Clicks"] + ["Price"] + ["CTR"],
  24. ascending=[False,True,False])
  25. )
  26.  
  27. df_out = group_concat(df_out, GR_COLS, "impressions")
  28. df_out.rename(columns={'impressions': 'item_recommendations'}, inplace=True)
  29.  
  30. return df_out
RAW Paste Data