pythonloopsprogress-barjupyterliterate-programming

Why the loop is not iterating each row?


I have a dataframe offer_received_datalike this:

            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    

and a dataframe portfolio like this:

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

Here is my code:

# 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)

This returns:

9b98b8c7a33c4b65b9aebfe6a799e6d9

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

It's only returning one row, not iterating each row at all, can someone have a look at my code, what have I done wrong? Many thanks.


Solution

  • Your return is within the iteration loop. Either move it out of the loop or change it for a yield