I have a DateFrame:
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.
>Solution :
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

