Skip to content

Instantly share code, notes, and snippets.

@Alakhator
Created May 18, 2020 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Alakhator/dda94e03609a7d7b31b3bc924c40952f to your computer and use it in GitHub Desktop.
Save Alakhator/dda94e03609a7d7b31b3bc924c40952f to your computer and use it in GitHub Desktop.
train["User_ID_MeanPrice"] = train.groupby(['User_ID'])['Purchase'].transform('mean')
userID_mean_dict = train.groupby(['User_ID'])['Purchase'].mean().to_dict()
test['User_ID_MeanPrice'] = test['User_ID'].apply(lambda x:userID_mean_dict.get(x,0))
train["Product_ID_MeanPrice"] = train.groupby(['Product_ID'])['Purchase'].transform('mean')
productID_mean_dict = train.groupby(['Product_ID'])['Purchase'].mean().to_dict()
test['Product_ID_MeanPrice'] = test['Product_ID'].apply(lambda x:productID_mean_dict.get(x,0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment