0

I have a column in a data frame with the following left side format. I reckon that because of the , and the $ symbol the column has a string format, but I want to convert it into float, in order to manipulate the data.

I have used the two following lines of code to strip those characters, hoping it would convert the column values into floats.

sales_dataset_date['invoice_value2'] = sales_dataset_date['invoice_value'].replace('$', '')
sales_dataset_date['invoice_value2'] = sales_dataset_date['invoice_value'].replace(',', '')

After I run the above commands, I get the following output:

|invoice_value |invoice_value2 |
| $55.87       | $55.87        |
| $28,852.37   | $28,852.37    |
| $1,392.00    | $1,392.00     |

I can't convert it into float. There is no difference with the replace command. What I am doing wrong? How can I fix it?

accdias
  • 5,160
  • 3
  • 19
  • 31
azinsera
  • 13
  • 2
  • Does this answer your question? [converting currency with $ to numbers in Python pandas](https://stackoverflow.com/questions/32464280/converting-currency-with-to-numbers-in-python-pandas) – accdias Jun 10 '23 at 18:58
  • Anyway, the answer you are looking for is `sales_dataset_date['invoice_value2'] = sales_dataset_date['invoice_value'].replace('[\$,]', '', regex=True).astype(float)`. – accdias Jun 10 '23 at 19:01
  • Thak you very much. I appreciate it – azinsera Jun 10 '23 at 19:41

1 Answers1

0

Try this:

df['invoice_value2']=df['invoice_value2'].replace('[\$,]','', regex=True).astype(float)
gtomer
  • 5,643
  • 1
  • 10
  • 21