Error to Convert the number of reviews to float

Screen Link: https://app.dataquest.io/m/350/guided-project%3A-profitable-app-profiles-for-the-app-store-and-google-play-markets/5/removing-duplicate-entries-part-two

My Code:

reviews_max = {}
for app in android_apps[1:] :
    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(len(reviews_max))

What I expected to happen: Convert the number of reviews to float

What happened:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-50-8ee4f6d998b8> in <module>
      2 for app in android_apps[1:] :
      3     name  = app[0]
----> 4     n_reviews = float(app[3])
      5     if (name in reviews_max) and (reviews_max[name] < n_reviews)  :
      6         reviews_max[name] = n_reviews

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

Without convert I have a good result and when I remove the function float to app[3] and I do print(float(n_reviews)) no error . So I want to understand why I can’t convert app[3] . Thank to explain me

Hello, check the solution here Python fundamentals guided Project

Hi @biadboze!

The reason is clarified in the error you receive -

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

The error above points out the particular value at which your code is unable to convert the string, 3.0M, to float.

As you already know, floats are only numbers. However, the strings contained in app[3] also include that M that you see at the end of the above error. Since M can’t be converted to a number, you get an error.

So, in order to be able to convert those strings into floats, you will have to make sure that there are no other non-numeric characters in those strings.

Hope this helps!

1 Like

thanks for the link .

Thank you , for the explanation.

Thanks so much, this really help

Hi there, I am struggling with the same issue and went through the code several times (also checking the answers in the community forum) but I still have no idea what is going wrong with the code.

I cleared the output and run all the cells again to avoid any problems with the code overlaying previously saved one.

I checked if the row I’m removing is the correct one by writing a function to identify the row with a different length than the header row. I also run the function again to see if Python finds the incorrect row again and unfortunately it does (and I don’t know why that is happening).

Still after that I receive the same error.

Below you can find the code

Untitled1.ipynb (14.4 KB)

Thanks for any advice!

Click here to view the jupyter notebook file in a new tab

Hi, @skinderella.

The incorrect row is at 10472. But you are deleting the row 10742

del google[10742]

You are deleting the wrong row. Once you fix that, you should not face any problems.

I checked your notebook, you made an error on the index. you deleted index 10742 instead of 10472.

del google[10742]

Your solution was in your project and a little advice accompany your work with words (documentation) to make your project readable, don’t be only focus on the code :wink:

Thanks so much to both of you! I was sitting yesterday and pulling my hair out with this error :pray:

@biadboze thank oyu for the tip - some people already told me that but I still keep forgetting that the comments are also importand :slight_smile:

1 Like

Hi! Professor, I have the exact same issue and I tried to use this code : del apps_data_google[10473] to delete the 3.0M data, however, it keeps popping out the error of “List assignment index out of range” but I have checked my data length already and it is 10841, so Im very confused now,… other than that, I got another question which is, if the error tells us : there s a 3.0M so it couldn’t be converted to float, then how should we locate the wrong data, like how Do I know it is in the row 10473?? Thank you so much!

Not a Professor. Just a fellow learner/student. :slightly_smiling_face:

But please create a separate new post for your question. It’s different than the main question of this post, so it’s best to have a different post.

ok thank you so much!!

Hi There
hope you are well
Apologies I am having the exact same issue. I have tried to run it again

ValueErrorTraceback (most recent call last)
in ()
3 for app in android:
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: ‘Reviews’

I have attached my note book can you please assist?
Basics (1).ipynb (11.3 KB)
Many thanks!

Bosh

Click here to view the jupyter notebook file in a new tab

First of all, think backwards from the error. The error states -

ValueError: could not convert string to float: ‘Reviews’

It says it could not convert string to float, and then points out the string it could not convert - Reviews.

Now, spend some time and think about where you would get the string Reviews in your data at that particular position (app[3])? Take your time, but think about it.

Hi @the_doctor,
Thanks for your reply.

RE" It says it could not convert string to float, and then points out the string it could not convert - Reviews ."

That is why I am confused the code I entered does not have Reviews anywhere in it.
android_clean =
already_added =

for app in android:
name = app[0]
n_reviews = float(app[3])
if (n_reviews == max_reviews[name]) and (name not in already_added):
android_clean.append(app)
already_added.append(name)

This matches what the answers have but it still won’t process.
Can you elaborate more please?

Thanks

Bosh

Hi @the_doctor ,

Apollogies I understand what you are saying now.

It/s because i have included the column title. I had this initially but got confused it was not what the solution page shows.
Thanks for your help!
Bosh

1 Like