40

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
  • 96,739
  • 27
  • 183
  • 184
Amani
  • 13,566
  • 21
  • 88
  • 140

3 Answers3

53

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
  • 96,739
  • 27
  • 183
  • 184
  • 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
17

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
  • 35,621
  • 16
  • 134
  • 143
4

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',''))