Home > OS >  How can I iterate in my dataframe putting an extra column and a value if some conditions are accompl
How can I iterate in my dataframe putting an extra column and a value if some conditions are accompl

Time:05-20

I have a dataframe with over 8 millions records like this, product promotion It´s what you have to buy to get your reward, accumulation It´s what you have bought.

Name Product Product Promotion Reward Accumulation
Jhon Prod1 3 1 3
Casandra Prod2 2 1 1

To cover any situation, I have a code, If some other conditions are met, too far I have a code like this but I´ts taking too long and I´m not getting the results I want to have.

for acummulation, promotion,reward in zip(df["Accumulation"], df["Product Promotion"],df["Reward"]):        
    if acummulation==reward:       
        df['ProductToRetire']=Reward
    elif acummulation>reward:
        df['ProductToRetire']=math.trunc(acummulation/promotion)*Reward
    else:
        df['ProductToRetire']=0

So my question is, It´s there anyway to itirate this in a simpler way?

CodePudding user response:

You can try np.select

df['ProductToRetire'] = np.select(
    [df['Accumulation'].eq(df['Reward']),
     df['Accumulation'].gt(df['Reward'])],
    [Reward,
     df['Accumulation'].div(df['Product Promotion']).apply(math.trunc).mul(Reward)],
    default=0
)
  • Related