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

Changing datatype and removing commas from a number

I am trying to remove commas from number which has been entered as a string and change it’s data type to integer for analysis purposes

0      1,741,147
1          1,755
2         95,532
3        216,022
4        208,134

Above is the Pandas series(‘Active Cases’) that I am working with whose data type is Object. I want to remove the commas and change the data type to int64. I have tried doing it in the following way but it is not working:

df['Active Cases'].replace(',','')
df['Active Cases'].astype(np.int64)

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

>Solution :

The operation is not inplace, you need assign the result back

df['Active Cases'] = df['Active Cases'].replace(',','').astype(np.int64)

If you want to make the replace more robust, like also replace possible spaces after comma, you can do Series.str.replace

df['Active Cases'] = df['Active Cases'].str.replace(', *', '', regex=True).astype(np.int64)

I would recommend do pd.to_numeric instead to avoid possible error

df['Active Cases'] = pd.to_numeric(df['Active Cases'].str.replace(', *', '', regex=True), errors='coerce')
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