Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove .0 from number

I have one column DOB(Year) in df dataframe, which consist values like below:

DOB(Year)
1990.0
1998.0
2015.0
2017.0

I want to remove .0 from all values.

I have tried

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df[DOB(Year)]=df[DOB(Year)].astype(str)
df[DOB(Year)]=df[DOB(Year)].str.replace(".0$", "",regex=True)

But resulting column values are nan.
Can anyone please suggest solution for this?

>Solution :

If you want a safe method that works on numeric/string input:

df['DOB(Year)'] = (pd.to_numeric(df['DOB(Year)'], errors='coerce')
                     .round().convert_dtypes()
                  )

Example (as new column):

   DOB(Year)  DOB(Year)_converted
0     1990.0                 1990
1     1998.0                 1998
2     2015.0                 2015
3     2017.0                 2017
4  2011.0001                 2011
5        abc                 <NA>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading