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
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 :)
Yes, this worked pretty well:
python 0 2.0000000000000000 2.0000000000000000 -14,914.6227557955608063 1 3.0000000000000000 2.0000000000000000 -10,563.3651604968417814 2 4.0000000000000000 2.0000000000000000 -6,927.2062998872152093
Thank you so much!Awesome, remember to accept the answer if it worked for you since this can help others. Thanks :)