Act fast, special offers end soon! Up to $294 is savings when you get Premium today.
Get offer codes

I don't know why str cant be converted to float

I am working on the first guided project… I want to convert a str type to float the syntax looks right to my understanding but its giving me the error below:

reviews_max = {}

for app in my_googleApps:
    name = app[0]
    n_reviews = float(app[3])
    
    if name in reviews_max and reviews_max[name] < n_reviews:
        reviews_max[name] = n_reviews
        
    elif name not in reviews_max:
        reviews_max[name] = n_reviews

ValueErrorTraceback (most recent call last)
<ipython-input-15-597f037c44ec> in <module>()
      3 for app in my_googleApps:
      4     name = app[0]
----> 5     n_reviews = float(app[3])
      6 
      7     if name in reviews_max and reviews_max[name] < n_reviews:

ValueError: could not convert string to float: '3.0M'

Please help …

Regards,
Olubaji

1 Like

Hi olubaji22,

If you look at the error message you can see that it is giving you a clue to where the problem is.

ValueError: could not convert string to float: ‘3.0M’

It is saying that somewhere in the data we are trying to convert to a float there is a string ‘3.0M’ that it cannot convert. The problem is the ‘M’ at the end of the string. For this project, the best way to fix this problem is to just delete the row with the problem data.

del android[10472] # don’t run this more than once

As explained in the guided project, the following post explains that there is an error for entry 10472 (that’s why we are deleting that entry).

1 Like

Thank you very much @bvalgard.

I already deleted 10472, guess I must have deleted the wrong row…lol

I guess I’ll reset my progress and start over so my figures will tally with the guided project solution, Or do you have another suggestion for me @bvalgard.

You’re welcome.

Your plan sounds like a good one to me. In later missions on Dataquest you will learn exciting techniques to deal with problems like this, but for now I would say stick to what you know.

Seems there was a problem with the terminal when I deleted the row. So it was not effected. Guess I lost internet connection. Cos the number still tally

reviews_max = {}

for app in my_googleApps:
    name = app[0]
    n_reviews = float(app[3])
    
    if name in reviews_max and reviews_max[name] < n_reviews:
        reviews_max[name] = n_reviews
        
    elif name not in reviews_max:
        reviews_max[name] = n_reviews
        
print('Expected length:', len(my_googleApps) - 1181)
print('Actual length:', len(reviews_max))

Expected length: 9659
Actual length: 9659

Thanks again @bvalgard

Hello I’m having the same issue, unfourtuntly, I run the code of deleting the error more than once
how can I solve it, please?

Just re-run all code cells in your Notebook from the beginning. That will ensure everything gets “reset”.

That’s the most straightforward way.

The alternative is to pay close attention to what variables you are defining, where you are defining them, and where you update them. Based on that you can try to run only the relevant code cells instead of running all of them. By relevant code cells, I mean the ones which impacted the variable you want to “fix”.

For now, just run all of them from the beginning. But try to keep the 2nd suggestion (the alternative) in mind because, as per me, it’s a good exercise to be able to keep track of what your code does and manipulate it if needed. Don’t worry if the latter is confusing for now. With more experience, you should reach that point on your own as well.