I want to check how many times a customer has done transaction with the same merchant in the last 1 month. I wrote code.Need to optimise it

I want to check how many times a customer has done transaction with the same merchant in the last 1 month. I wrote code.Need to optimise it

    df["EVENT_DATE"] = pd.to_datetime(df["EVENT_DATE"])

    df = df.sort_values(["CUST_CARD_ID", "EVENT_DATE"], ascending=[True, True])
    df.reset_index()

    a=0    

    c=0

    for i in range(len(df)):

        a = df.loc[i,"EVENT_DATE"]
        
        a = a - datetime.timedelta(minutes=43829)

        c=0

        for j in range(i):
    
            if (df.loc[i,"CUST_CARD_ID"] == df.loc[j,"CUST_CARD_ID"] and 
            
            df.loc[i,"MERCHANT_ID"] == df.loc[j,"MERCHANT_ID"] and 

            df.loc[j,"EVENT_DATE"] >= a and df.loc[j,"EVENT_DATE"] < 
            
            df.loc[i,"EVENT_DATE"]):
        
                     c = c+1

         df.loc[i,"NO_OF_TRANSACTION"] = c

   print(df.NO_OF_TRANSACTION)
   

Source: Python Questions

LEAVE A COMMENT