我正在尝试根据第三列中的计数查找每种LibraryID类别(例如3)的顶部Isbns。需要逻辑方面的帮助。
这是一个推荐系统,它基于顾客的结帐次数列出在线图书馆中的热门书籍。我有一个数据框,可以从中enter code here
获取此信息。
curr.execute("SELECT c.LibraryID, o.ISBN, count(c.ItemID) FROM
CheckoutTransactionArchive c left join OMNI o ON c.ItemID = o.BTKey
WHERE cast(ActionStartDate as Date) between '20170101' and
'20181231' GROUP BY c.LibraryID, o.ISBN ORDER BY c.LibraryID,
o.ISBN;")
inputrows = pd.DataFrame(curr.fetchall())
inputrows.columns = ["LibraryID", "ISBN", "Checkout count"]
inputrows = inputrows.head(20000)
test_dict = {}
j = 1
i=0
while i< len(inputrows):
library_id = inputrows.iloc[i,0]
next_library_id = inputrows.iloc[i+1, 0]
isbns = []
counts = []
while next_library_id == library_id :
isbn = inputrows.iloc[j, 1]
count = inputrows.iloc[j, 2]
isbns.append(isbn)
counts.append(count)
next_library_id = inputrows.iloc[j, 0]
library_id = inputrows.iloc[i,0]
j+=1
i+=1
if next_library_id != library_id:
i+=1
j+=1
数据框如下所示:
LibraryID ISBN COUNT
1 1 3
1 2 2
1 3 1
1 4 3
1 5 3
1 6 34
2 7 3
2 8 12
2 9 10
3 10 3
4 11 1
4 12 3
我想要Python中以LibraryID为键的字典,并且基于计数,值将排在前n个ISBNS上。 例如,这里的字典看起来像这样。 用于最多前三本书
{1: [6,5,4],
2: [9,8,7],
3:[10],
4 : [11,12]}
在前十名中,顺序并不重要。