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

Pandas : Changing a column of dataset from string to integer

One Column of my dataset is like this:

0        10,000+
1       500,000+
2     5,000,000+
3    50,000,000+
4       100,000+
Name: Installs, dtype: object

and I want to change these ‘xxx,yyy,zzz+’ strings to integers.
first I tried this function:

df['Installs'] = pd.to_numeric(df['Installs'])

and I got this error:

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

ValueError: Unable to parse string "10,000" at position 0

and then I tried to remove ‘+’ and ‘,’ with this method:

df['Installs'] = df['Installs'].str.replace('+','',regex = True)
df['Installs'] = df['Installs'].str.replace(',','',regex = True)

but nothing changed!

How can I convert these strings to integers?

>Solution :

+ is not a valid regex, use:

df['Installs'] = pd.to_numeric(df['Installs'].str.replace(r'\D', '', regex=True))
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