Home > Net >  If conditions Pandas
If conditions Pandas

Time:07-24

all my values are int in my data frame and I am trying to do this if condition

if the value is greater than 1, than multiply by 1 else multiple the value with -1 and add to a new column, but it gives error

'>' not supported between instances of 'str' and 'int'

below is the code I wrote

Cfile["Equ"] = [i*1 if i>1 else i*-1 for i in Cfile["Net Salary"]]

CodePudding user response:

s = df['Net Salary']
df['Equ'] = np.where(s.gt(1), s, s.mul(-1))

CodePudding user response:

Use from this code:

df['new'] = (df.net>1)*df.net - (1≥df.net)*df.net
  • Related