Home > Net >  Values changed after converting object type column to string column type Python
Values changed after converting object type column to string column type Python

Time:08-19

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.

CodePudding user response:

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

df['New selling price'] = df['New selling price'].astype(float).round(5).astype('string')
  • Related