Warm tip: This article is reproduced from stackoverflow.com, please click
pandas python text decimal

Conserving upto 16 decimal places in Python

发布于 2020-03-27 10:28:32

I have a table of numerical in a text file with 16-digit decimal precision that I am reading into a Jupyter Notebook and converting it into a Pandas DataFrame. How can I use the decimal module with pandas to preserve the precision from the text file? Or is there anything else that would work better? Currently, it is showing only up to 5-6 decimal places. Thanks!

code:

from pathlib import Path
folder = Path("C:/This/is/the/path")
file = folder1/"thisfile.txt"
df = loadtxt(file1)
df = pd.DataFrame(df)

The input is a text file:

                2                    2  -14914.622755795561     
                3                    2  -10563.365160496842     
                4                    2  -6927.2062998872152     
                5                    2  -3886.5429891705344     
                6                    2  -1370.9348975732908     

The output looks like this in pandas:

0   2.0 2.0 -14914.622756
1   3.0 2.0 -10563.365160
2   4.0 2.0 -6927.206300
3   5.0 2.0 -3886.542989
4   6.0 2.0 -1370.934898
Questioner
ak-dev
Viewed
50
Shiva Kishore 2019-07-03 23:29

Pandas doesn't reduce the precision of your data, but it just rounds it off while displaying.

To get 16-digit decimal precision, set the display formatting of float to 16 digits.

pd.options.display.float_format = '{:,.16f}'.format

Use the above configuration after importing pandas.

Happy Coding :)