Menu Close

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

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.

Posted in Python, Python Answers