if else condition in pandas with multiple column as arguements

Question:

I have a df like so:

import pandas as pd
df = pd.DataFrame({"code": [sp,wh,sp], "qty": [20, 30, 10]})

I want to create a new column based on data from the two columns with the value the new column as the same as an existing column if a condition is met. This is what I’ve tried:

df['out'] = df.apply(lambda x: x['qty']) if x['code'] == 'sp' else 0)

so my output in this case should be:

df = [
{'code':'sp', 'qty':20, 'out':20}
{'code':'wh', 'qty':30, 'out':0}
{'code':'sp', 'qty':10, 'out':10}
]
Asked By: Tony

||

Answers:

You can use numpy’s where:

import numpy as np
df['out'] = np.where(df['code']=='sp', df['qty'], 0)
Answered By: gtomer

here is one way to do it using mask

# mask the qty as zero when code is not 'sp'

df['out']=df['qty'].mask(df['code'].ne('sp'), 0)
df
code    qty     out
0   sp  20  20
1   wh  30  0
2   sp  10  10

Answered By: Naveed

You just need to add the parameter axis=1 in the apply() method in order to apply the method on the columns :

df['out'] = df.apply(lambda x: x['qty'] if x['code'] == 'sp' else 0, axis=1)
Answered By: Pierre-Loic
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.