0

I have a DateFrame:

enter image description here

what I am trying to achieve is to do add 2-3 columns after checking specific conditions

The condition is

limit_a = 1000
limit_b = 500
limit_c = 2000

if df['input_amount']>limit_a and df['type']=="A":
    df['approved amount'] = limit_a
    df['comment'] = 'limit reached for A'
else:
    df['approved amount'] = df['input_amount']
    df['comment'] = 'limit not reached for A'

if df['input_amount']>limit_b and df['type']=="B":
    df['approved amount'] = limit_b
    df['comment'] = 'limit reached for B'
else:
    df['approved amount'] = df['input_amount']
    df['comment'] = 'limit not reached for B' 

This wasn't working for me.

This will be the resulting output.

enter image description here

2 Answers 2

1

One way using map and min:

limits = {"A":1000,"B": 500, "C":2000}

df["approved_amount"] = df["type"].map(limits)
df["approved_amount"] = df[["input_amount", "approved_amount"]].min(axis=1)

Output:

   input_amount type  approved_amount
0          2000    A             1000
1           300    B              300
2           526    A              526
3          1000    A             1000
4          1500    C             1500
5          1350    B              500
Sign up to request clarification or add additional context in comments.

1 Comment

I have edited the question. Can you take a look?
1

Use Series.clip with Series.map, for new column comment compare columns for not equal and set new values by numpy.where:

limit_a = 1000
limit_b = 500
limit_c = 2000
d = {'A':limit_a, 'B':limit_b, 'C':limit_c}
df['approved_amount']  = df['input_amount'].clip(upper=df['type'].map(d))

m = df['approved_amount'].ne(df['input_amount'])
df['comment'] = np.where(m, 'limit reached for ', 'limit not reached for ') + df['type']
print (df)
   input_amount type  approved_amount  approved amount  \
0          2000    A             1000             1000   
1           300    B              300              300   
2           526    A              526              526   
3          1000    A             1000             1000   
4          1500    C             1500             1500   
5          1350    B              500              500   

                   comment  
0      limit reached for A  
1  limit not reached for B  
2  limit not reached for A  
3  limit not reached for A  
4  limit not reached for C  
5      limit reached for B  

2 Comments

I have edited the question. Can you take a look?
@ShijoJ - Answer was edited.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.