Home > Back-end >  How do I calculate the transaction amount which is divisible by 10 in pandas
How do I calculate the transaction amount which is divisible by 10 in pandas

Time:11-26

I want to create a new column and assign 0 or 1 based on the condition i.e. if transaction_amount is divisible 10. Transaction_amount is one of the columns from df.

Tried the below code but it is not working.

df = df.assign(whole_amt = lambda x: 1 if (x.transaction_amount%10==0) else 0,axis=1)

CodePudding user response:

Use Series.divmod:

df['whole_amt'] = df['transaction_amount'].mod(10).eq(0).astype(int)

CodePudding user response:

df['div10'] = df['transaction_amount'].apply(lambda x: 1 if x % 10 == 0 else 0)

CodePudding user response:

df = pd.DataFrame({'transaction_amount': [1, 22, 30, 44, 50]})
df = df.assign(whole_amt=lambda x: 1 - (x.transaction_amount % 10).clip(0, 1))
print(df)
   transaction_amount  whole_amt
0                   1          0
1                  22          0
2                  30          1
3                  44          0
4                  50          1
  • Related