首页 > 解决方案 > 为什么循环不迭代每一行?

问题描述

我有一个offer_received_data这样的数据框:

            customer_id                 offer_id                              time  offer_received offer_viewed 
0   78afa995795e4d85b5d9ceeca43f5fef    9b98b8c7a33c4b65b9aebfe6a799e6d9       0.0    1            0    
53176   78afa995795e4d85b5d9ceeca43f5fef    5a8bc65990b245e5a138643cd4eb9837   7.0    1            0    
150598  78afa995795e4d85b5d9ceeca43f5fef    ae264e3637204a6fb9bb56bc8210ddfd   17.0   1            0    

和这样的数据框portfolio

offer_id    reward  difficulty  duration    informational   discount    bogo    mobile  social  web
0   ae264e3637204a6fb9bb56bc8210ddfd    10  10  7   0   0   1   1   1   0

这是我的代码:

# loop through each received offer
def get_offer_row(offer_received_data, portfolio):
    
    offer_row = []
    
    widgets=[
        ' [', progressbar.Timer(), '] ',
        progressbar.Bar(),
        ' (', progressbar.ETA(), ') ',
    ]
    
    for ind in progressbar.progressbar(range(len(offer_received_data)), widgets=widgets):
        
        for i in range(offer_received_data.shape[0]):
            
    # fetch an offer id 
            offer_id = offer_received_data.iloc[i]['offer_id']
            print(offer_id)   
    # get offer_id row from portfolio
            offer_row = portfolio.loc[portfolio['offer_id'] == offer_id]

            return offer_row
get_offer_row(offer_received_data, portfolio)

这将返回:

9b98b8c7a33c4b65b9aebfe6a799e6d9

offer_id    reward  difficulty  duration    informational   discount    bogo    mobile  social  web
3   9b98b8c7a33c4b65b9aebfe6a799e6d9    5   5   7   0   0   1   1   0   1

它只返回一行,根本不迭代每一行,有人可以看看我的代码,我做错了什么?非常感谢。

标签: pythonloopsprogress-barjupyterliterate-programming

解决方案


return在迭代循环中。将其移出循环或将其更改为yield


推荐阅读