Python Forum
Increase df column values decimals - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Increase df column values decimals (/thread-38615.html)



Increase df column values decimals - SriRajesh - Nov-05-2022

Hi,
I have below datframe with numerical column marks
df:
name age marks
A       14   12.34
B       12   34.1
C       29   19.567
I want to increase marks column decimals to 6

desired output:


name age marks
A       14   12.340000
B       12   34.100000
C       29   19.567000
I use below code but it did not change anything:
df['marks'] =df['marks'].round(6)



RE: Increase df column values decimals - snippsat - Nov-05-2022

import pandas as pd
from io import StringIO

data = StringIO('''\
name age marks
A 14 12.34
B 12 34.1
C 29 19.567''')

df = pd.read_csv(data, sep=' ')
>>> df
  name  age   marks
0    A   14  12.340
1    B   12  34.100
2    C   29  19.567

>>> df['marks'] = df['marks'].map(lambda x: f'{x:.6f}')
>>> df
  name  age      marks
0    A   14  12.340000
1    B   12  34.100000
2    C   29  19.567000



RE: Increase df column values decimals - deanhystad - Nov-14-2022

You can set the number of significant digits pandas uses when printing.
import pandas as pd
pd.set_option('display.float_format', '{:.10f}'.format)

df = pd.DataFrame([1/x for x in range(1, 11)], columns=["values"])
df.to_csv("test.csv", index=False)
print(df)
Output:
0 1.0000000000 1 0.5000000000 2 0.3333333333 3 0.2500000000 4 0.2000000000 5 0.1666666667 6 0.1428571429 7 0.1250000000 8 0.1111111111 9 0.1000000000
Unlike snippsat's solution this only changes how floats are printed, the values in the dataframe are unaltered.