0

I have a Series of strings that represent cost data. I am attempting to convert the strings to floats

df['Line Total'].map(float)

which results in

ValueError: invalid literal for float(): 3,300.00

3,300.00 is the value from the first row of data.

I ran df['Line Total'].map(repr) based on the suggestion from here python ValueError: invalid literal for float() but I don't see any extra or unnecessary characters in my data:

enter image description here

Thanks in advance for any help.

Community
  • 1
  • 1
davidheller
  • 323
  • 1
  • 2
  • 11
  • You don't see extra characters? How about the comma? That can't be parsed with a number. – TigerhawkT3 Jul 14 '16 at 00:05
  • I didn't identify the comma as an extra character which is what the problem was. Seems obvious in hindsight but that is usually the case. – davidheller Jul 14 '16 at 17:48
  • i agree though that the other question covers it. I spent quite a bit of time searching for an answer but the problem was not seeing that the comma was causing the problem. – davidheller Jul 14 '16 at 17:52

1 Answers1

0

This had me stuck for awhile but thanks to this Parse String to Float or Int I was able to figure out that commas in the string were the problem.

I ran df['Line Total'] = df['Line Total'].str.replace(',', '') and that took care of the problem!

Community
  • 1
  • 1
davidheller
  • 323
  • 1
  • 2
  • 11