0

I need to convert a data frame column containing a string to a float.

Typical string contains "," separators e.g. "1,000.00", so the commas need to be stripped out.

This does not work

dataframe.sales_aud = dataframe.sales_aud.replace(",", "")

dataframe.astype({"units": int, "sales_aud": float})

Error below:

 ValueError                                Traceback (most recent call last)
    <ipython-input-9-669177671309> in <module>
         17 dataframe.sales_aud = dataframe.sales_aud.replace(",", "")
         18 
    ---> 19 dataframe.astype({"units": int, "sales_aud": float})
         20 
         21 dataframe.to_pickle("weekly_sales_2020.csv")
....
ValueError: could not convert string to float: '1,088'
Duncan Groenewald
  • 8,496
  • 6
  • 41
  • 76
  • This does not work is not an accurate description please tell why it does not work? – Poojan Mar 23 '20 at 23:18
  • @Poojan see error – Duncan Groenewald Mar 23 '20 at 23:21
  • Does this answer your question? [Convert number strings with commas in pandas DataFrame to float](https://stackoverflow.com/questions/22137723/convert-number-strings-with-commas-in-pandas-dataframe-to-float) – AMC Mar 23 '20 at 23:49

1 Answers1

0

I just had the syntax incorrect.

dataframe['sales_aud'] = dataframe['sales_aud'].str.replace(",","")
Duncan Groenewald
  • 8,496
  • 6
  • 41
  • 76