如何在while循环中迭代

2024-10-02 02:32:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图根据第三列中的计数,找出图书馆ID中每个类别(比如3个)的最高ISBN。需要逻辑方面的帮助

这是一个推荐系统,它根据用户的退房次数列出在线图书馆的顶级图书。我有一个数据帧,我从中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个ISBN。 例如,这里的字典看起来像这样。 最多可阅读前3本书

{1: [6,5,4],
2: [9,8,7],
3:[10],
4 : [11,12]}

在前十名中,顺序并不重要


Tags: 数据idby图书馆countlibrarynext计数

热门问题