Categories
Python Answers

How to convert number strings with commas in Python Pandas DataFrame to float?

Spread the love

To convert number strings with commas in Python Pandas DataFrame to float, we can use the astype method.

For instance, we write

df['colname'] = df['colname'].str.replace(',', '').astype(float)

to convert the values in the data frame df‘s colname column to a float by removing the commas from the strings with str.replace.

And then we call astype with float to convert the number strings with the commas removed to floats.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *