Python Forum
Increase df column values decimals
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Increase df column values decimals
#1
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)
Reply
#2
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
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  attempt to split values from within a dataframe column mbrown009 8 2,223 Apr-10-2023, 02:06 AM
Last Post: mbrown009
  pandas: Compute the % of the unique values in a column JaneTan 1 1,758 Oct-25-2021, 07:55 PM
Last Post: jefsummers
  [Pandas] Write data to Excel with dot decimals manonB 1 5,774 May-05-2021, 05:28 PM
Last Post: ibreeden
  Assigning Column nunique values to another DataFrame column Pythonito 0 1,685 Jun-25-2020, 05:04 PM
Last Post: Pythonito
  Counter to keep track how many times values in a list appear in each row in a column chief 0 1,574 Mar-24-2020, 08:14 PM
Last Post: chief
  sort values of a column pandas karlito 2 2,473 Oct-22-2019, 06:11 AM
Last Post: karlito
  Pandas Import CSV count between numerical values within 1 Column ptaylor520 3 2,606 Jul-16-2019, 08:13 AM
Last Post: ptaylor520
  How to delete column if entire column values are "nan" Sri 4 3,671 Apr-13-2019, 12:16 PM
Last Post: Sri
  Splitting values in column in a pandas dataframe based on a condition hey_arnold 1 4,139 Jul-24-2018, 02:18 PM
Last Post: hey_arnold
  Insert values into a column in the same table based on a criteria klllmmm 3 4,179 Apr-13-2017, 10:10 AM
Last Post: zivoni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020