Home > OS >  Convert pandas str column to float without loss of precision
Convert pandas str column to float without loss of precision

Time:04-09

im receiving this data from an API which in turn I put into a dataFrame object. everything works fine until I need to change some of the columns into float from their original str format. after using pd.to_numeric and pd.astype(float) and .apply(float) I saw that my data Is limited at 5-6 digits after the floating-point. here is the before:

               Price        Weight
Ticker                                
A         2.5280000000  0.1107699174
B       137.6500000000  0.0315685242
C       203.0600000000  0.1220829996
D         5.6920000000  0.0406895407

and here is after:

            Price    Weight
Ticker                       
A         2.52800  0.110770
B       137.65000  0.031569
C       203.06000  0.122083
D         5.69200  0.040690

although 6 digits is good, i need the entire data to perform percise calculations for model-making purposes. i would truly appreciate any help on this!

CodePudding user response:

By default pandas shows 6 digits after ., but that does not imply precision of stored value, if value is stored with enough precision then it should be enough to set pandas.options.display.precision value to greater value to see it, consider following example

import pandas as pd
df = pd.DataFrame({'x':[0.1107699174]})
print(df)
pd.options.display.precision = 12
print(df)

output

         x
0  0.11077
              x
0  0.1107699174
  • Related