close

How to remove decimal points in pandas

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to remove decimal points in pandas in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to remove decimal points in pandas?

  1. How to remove decimal points in pandas?

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

  2. remove decimal points in pandas

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

Method 1

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 

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

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read