49

I have a pandas data frame, df, which looks like this:

Cut-off             <=35   >35                   
Calcium              0.0   1.0
Copper               1.0   0.0
Helium               0.0   8.0
Hydrogen             0.0   1.0

How can I remove the decimal point so that the data frame looks like this:

Cut-off             <= 35  > 35                   
Calcium              0     1
Copper               1     0
Helium               0     8
Hydrogen             0     1

I have tried df.round(0) without success.

Alexander
  • 105,104
  • 32
  • 201
  • 196
Amani
  • 16,245
  • 29
  • 103
  • 153

3 Answers3

63

You have a few options...

1) convert everything to integers.

df.astype(int)
          <=35  >35
Cut-off            
Calcium      0    1
Copper       1    0
Helium       0    8
Hydrogen     0    1

2) Use round:

>>> df.round()
          <=35  >35
Cut-off            
Calcium      0    1
Copper       1    0
Helium       0    8
Hydrogen     0    1

but not always great...

>>> (df - .2).round()
          <=35  >35
Cut-off            
Calcium     -0    1
Copper       1   -0
Helium      -0    8
Hydrogen    -0    1

3) Change your display precision option in Pandas.

pd.set_option('precision', 0)

>>> df
          <=35  >35
Cut-off            
Calcium      0    1
Copper       1    0
Helium       0    8
Hydrogen     0    1 
Alexander
  • 105,104
  • 32
  • 201
  • 196
  • 2
    @alexander how would one apply any of these methods to one particular column? Would it be? `df[">35"].round()` – 3kstc Apr 11 '18 at 22:53
  • 1
    @3kstc Yes. Probably best to convert result to integer `df[">35"].round().astype(int)` – Alexander Apr 11 '18 at 23:44
  • 1
    For your last comment. Why do you need `round()` if you will use `astype(int)` anyway? And why does `round()` give me one decimal place even if I leave it as blank or put zero in? Thanks – Bowen Liu Oct 03 '18 at 16:11
  • `astype(int)` truncates the value, so `pd.Series([.9]).astype(int)` results in a value of zero. It would be one if you first rounded. – Alexander Oct 03 '18 at 16:14
21

Since pandas 0.17.1 you can set the displayed numerical precision by modifying the style of the particular data frame rather than setting the global option:

import pandas as pd
import numpy as np

np.random.seed(24)
df = pd.DataFrame(np.random.randn(5, 3), columns=list('ABC'))
df 

enter image description here

df.style.set_precision(2)

enter image description here

It is also possible to apply column specific styles

df.style.format({
    'A': '{:,.1f}'.format,
    'B': '{:,.3f}'.format,
})

enter image description here

joelostblom
  • 43,590
  • 17
  • 150
  • 159
2

You can alternatively use this code as well if you do not want decimals at all:

df['col'] = df['col'].astype(str).apply(lambda x: x.replace('.0',''))
  • 3
    This will incorrectly transform values like 0.01 to 01 – Diego F Medina Jul 08 '22 at 15:59
  • 1
    It will also convert your column to string, which may or may not be a problem. – pedrostrusso Aug 15 '22 at 20:37
  • To avoid the incorrect result quoted by @DiegoFMedina, I use the regex, like this: df['col'] = df['col'].astype(str).apply(lambda x: re.sub( r'\.0$', '', x) ) – André Carvalho Sep 09 '22 at 20:38
  • This is good if your column might have `nan` or `inf` in (and you never have values like 1.01 as Diego points out - typically a database extract with an optional foreign key). – Chris Dec 23 '22 at 11:32