Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Error could not convert string to float:
#1
Hey all,

Ive been working on creating plots and am trying to convert a column that had $4,312.00 as an Object instead of a float or integer.

In order to do so, I had to remove some characters with the following code

member_clean.AverageCheckingBalance = member_clean.AverageCheckingBalance.str.replace('$', '')

#strip spaces in TotalDepositBalanes which prevents it from being converted to a float
member_clean['AverageCheckingBalance'] = member_clean['AverageCheckingBalance'].str.strip()
member_clean.AverageCheckingBalance = member_clean.AverageCheckingBalance.str.replace(',', '')
member_clean.AverageCheckingBalance = member_clean.AverageCheckingBalance.str.replace(' ', '')

member_clean['AverageCheckingBalance'] = member_clean['AverageCheckingBalance'].astype(float)

Basically, each character I removed with the code above besides the $, an error message told me to remove.

Now, after removing those characters, I'm getting an error message that says

Error could not convert string to float:

I checked the output of the column, and from what I can tell, it's all good. HEre's a sample.

Output:
0 498.09 1 79.40 3 6022.23 5 3024.94 6 548.34
I checked for null values, i think I checked for an empty line as well (not sure I did this one correctly, though) but nothing is coming up other than output similar to that posted above. Is there something wrong be the code Ive written to convert it to a float? I tried this with another column as well and I'm getting the same error, so I really think it has to be with the code and not the values, but I don't know for sure

Thank you.
Quote
#2
Isn't the value that is causing the error shown in the error text? Please post the full error text.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#3
(Oct-02-2019, 01:10 AM)ichabod801 Wrote: Isn't the value that is causing the error shown in the error text? Please post the full error text.

That’s what’s confusing me. It doesn’t list the value. I posted the full error message. Before, it did have the value that was causing the previous errors, but for this last one, it doesnt
Quote
#4
In line 8, what is the type of member_clean['AverageCheckingBalance']?
Did you try using float(member_clean['AverageCheckingBalance'] rather than astype?
Quote
#5
You can track down which items produce the error. Replace the last line with:

try:
    member_clean['AverageCheckingBalance'] = member_clean['AverageCheckingBalance'].astype(float)
except ValueError:
    print(repr(member_clean['AverageCheckingBalance'])
That will print all of the values causing an error.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  how to convert list into string Shevach 3 275 May-14-2019, 09:51 AM
Last Post: perfringo
  Convert string to a specific number of bytes artblinked 1 259 Mar-28-2019, 08:43 PM
Last Post: Larz60+
  ValueError: could not convert string to float: 'Pencil' Balakay97 3 2,930 Mar-08-2018, 07:30 PM
Last Post: sparkz_alot
  How to convert boolean "True" to custom string? student8 3 7,044 Oct-01-2017, 06:24 AM
Last Post: hbknjr
  pandas convert to tuple & float to float64 metalray 6 9,684 Feb-27-2017, 12:50 PM
Last Post: metalray

Forum Jump:


Users browsing this thread: 1 Guest(s)