I want to filter the DF
by checking the PCS
Column if ==0
and ITEM DESC =='ITEM XL'
. If the criteria is met, I will multiply it by the BOX
value and multiply it by 48
.
DF
ITEM DESC PCS BOX
ITEM S 12 -
ITEM M 12 -
ITEM L 12 -
ITEM XS 12 -
ITEM XL 0 1
CODES
if DF[DF['PCS']==0] & DF[DF['ITEM DESC']=='ITEM XL'] :
DF['PCS'] = DF['PCS'] * DF['BOX'] * 48
else:
DF['PCS']
ERROR
TypeError: unsupported operand type(s) for &: 'DatetimeArray' and 'DatetimeArray'
CodePudding user response:
Either you meant all
:
if DF[(DF['PCS']==0) & (DF['ITEM DESC']=='ITEM XL')].all():
DF['PCS'] = DF['PCS'] * DF['BOX'] * 48
Or any
:
if DF[(DF['PCS']==0) & (DF['ITEM DESC']=='ITEM XL')].any():
DF['PCS'] = DF['PCS'] * DF['BOX'] * 48
Or more probable that you want to do it to specific rows:
DF.loc[(DF['PCS']==0) & (DF['ITEM DESC']=='ITEM XL'), 'PCS'] = DF['PCS'] * DF['BOX'] * 48
CodePudding user response:
Try using .apply
DF['PCS'] = DF.apply(lambda x: x.DF['PCS'] * x.DF['BOX'] * 48 if x.DF['PCS']==0 and x.DF['ITEM DESC']=='ITEM XL' else x.DF['PCS']==0, axis=1)