Skip to content

Instantly share code, notes, and snippets.

@hoyajigi
Created January 11, 2020 09:00
Show Gist options
  • Save hoyajigi/e0869c3723e3515880149398f898ff22 to your computer and use it in GitHub Desktop.
Save hoyajigi/e0869c3723e3515880149398f898ff22 to your computer and use it in GitHub Desktop.
import json
import pandas as pd
with open('input_p2.json') as json_file:
df = pd.DataFrame(json.loads(line) for line in json_file)
groupByProductId = df.groupby('product_id')
countOfDistinctUserId = groupByProductId['user_id'].nunique().sort_values()
print("Most popular product(s) based on the number of purchasers: ",
countOfDistinctUserId.loc[countOfDistinctUserId == countOfDistinctUserId.max()].index.tolist())
sumOfQuantity = groupByProductId.sum()
print("Most popular product(s) based on the quantity of goods sold: ",
sumOfQuantity.loc[sumOfQuantity['quantity'] == sumOfQuantity['quantity'].max()].index.tolist())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment