Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add multiple columns after pandas if-else if condition

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading