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 ive 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}
]
CodePudding user response:
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)
CodePudding user response:
You can use numpy's where:
import numpy as np
df['out'] = np.where(df['code']=='sp', df['qty'], 0)
CodePudding user response:
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