1

I have two columns in my pandas dataframe

Current selling price      New selling price
  0.0374                         0.03927
  0.1154                         0.12117
  0.0424                         0.04452
  0.1154                         0.12117
  0.1062                         0.11151
  0.035                          0.03675

Both the column are of type object. To do some analysis I want to convert them to string type so I used the below code.

df['Current selling price'] = df['Current selling price'].astype('string')
df['New selling price'] = df['New selling price'].astype('string')

After conversion some of the values in the column changed with long decimal values.

 Current selling price           New selling price
   0.0374                        0.039270000000000006
   0.1154                        0.12117000000000001    
   0.0424                        0.044520000000000004
   0.1154                        0.12117000000000001
   0.1062                        0.11151000000000001
   0.035                         0.036750000000000005  

Can anyone help me to solve this issue.

0

1 Answer 1

1

It is precision float problem, you can try round values:

df['New selling price'] = df['New selling price'].astype(float).round(5).astype('string')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.