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

Numbers in three different formats

I am working with a dataset; below you can see a small example.

import pandas as pd
import numpy as np

data = {
         'id':['9.','09', 9],
        }
df = pd.DataFrame(data, columns = [
                                      'id',])

df['id'] = df['id'].replace(".","")


df

In this data set, one number is written in three ways e.g '9.','09', 9
Now I want to have all of these numbers written in the same way e.g 9, without . or 0

I tried with this line of code but it is not working

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['id'] = df['id'].replace(".","")

So can anybody help me how to solve this problem?

>Solution :

try this:

pd.to_numeric(df['id'])
0    9.0
1    9.0
2    9.0
Name: id, dtype: float64

to int:

pd.to_numeric(df['id']).astype(int)
0    9
1    9
2    9
Name: id, dtype: int64
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