Bishu
Bishu

Reputation: 57

How do I calculate the transaction amount which is divisible by 10 in pandas

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)

Upvotes: 0

Views: 192

Answers (3)

Алексей Р
Алексей Р

Reputation: 7627

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

Upvotes: 0

prof_FL
prof_FL

Reputation: 162

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

Upvotes: 1

user17242583
user17242583

Reputation:

Use Series.divmod:

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

Upvotes: 1

Related Questions