samedi 14 décembre 2019

Pandas IF on previous column [duplicate]

This question already has an answer here:

I have a pd.DataFrame where the column contents are created based on formula applied to data in other columns. What I want to do is either not perform a calculation in the Su column at any given row if a certain criteria is met (either only calculate Su if SBTN is <=4, or zero the Su value after calculation, if that is easier.

Depth SBTN  Su
  1    1    4
  2    2    4
  3    5    0
  4    6    0
  5    2    4
  6    3    3

I have tried this to prevent the maths being done in the first place.

if df['SBTN']<=4: df['Su']= #do the maths

and this, after the calculation has been performed, to zero the cells retrospectively.

if df['SBTN']>=5: df['Su']= '0' #zero the cells after the maths has been done

Both give error messages 'The truth value of a series is ambiguous. Is there a neat way of resolving this without having to iterate through every row?

Aucun commentaire:

Enregistrer un commentaire