in

Reduce time complexity for double for loop


I am a student and need help in reducing time complexity of my below code. I have python dataframe df which contain customer id as customerid and their purchase date as createdon, in order to find the premium customer ,want to count max purchase for each customer in last 48 hr so created a 48 hr back threshold date as last_48 and ranked the buy date in asc order as buy_rank for each customer id. For this i have written the below code but it takes forever to run for 540k customerid. Please help to optimize my piece of code

%%time
c=[
for i in range(len(df)):
    k=0
    print(i)
    b=df[df['customerid']==df['customerid'].iloc[i]]
    for j in range(len(b)):
        if (df['customerid'].iloc[i]==b['customerid'].iloc[j]) & (df['last_48'].iloc[i] `<b['createdon'].iloc[j]) & (df['buy_rank'].iloc[i]>b['buy_rank'].iloc[j]):`
                k=k+1
            else:
                k
        c.append(k)

df[‘buy_count’]=c



Source: https://stackoverflow.com/questions/70716139/reduce-time-complexity-for-double-for-loop

RRB Mumbai NTPC Result and Cut-Off Marks 2021 Out

HotWax Commerce ionic commerce Hub App