Home > Blockchain >  Python unsupported operand types errors
Python unsupported operand types errors

Time:08-22

I have code:

ds['annual_inc_per_experience']= round(ds.annual_inc/ds.emp_length, 2)

and error: TypeError: unsupported operand type(s) for /: 'float' and 'str' I have no idea how to fix it.

Please help.

CodePudding user response:

You have string value in ds.emp_length. You might need to convert it to float or int first.

ds['annual_inc_per_experience'] = round(ds.annual_inc / float(ds.emp_length), 2)

CodePudding user response:

The TypeError itself has an idea of how to fix it, currently the ds.annual_inc is a type of float and ds.emp_length is a type of str so it is something like 1.0/"mass", that's why you are getting this error, you can cast the later one to float before the divide operation like below

ds['annual_inc_per_experience']= round(ds.annual_inc/float(ds.emp_length), 2)
  • Related