Error in my code project-1

reviews_max = {}

for app in android:
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

I got this error:

ValueError Traceback (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: ‘3.0M’

Please, advice!

@jehan.lina: It looks like the number of reviews, n_reviews cannot be converted to a floating point/decimal point number as it has a suffix of “M”, which I supposed is million. So you are trying to convert 3 million to a float but it doesn’t work due to the suffix as it is originally stored as a string. Thus, when you implicitly convert with the float() method, it throws an error.

I’m not too sure about which project you are referring to so perhaps you could provide a link or screenshot of the question, but I will still attempt at answering your question.

Alternatively, you may choose to use Python’s .strip() method to get rid of the suffix M, assuming all the number of review are in the millions for comparison in the if - elif block.

You can do so by doing:

reviews_max = {}
for app in android:
name = app[0]
n_reviews = float(app[3]).strip('M')

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

Hope this helps!

Hello @jehan.lina,

For the guided project you’re working on, the google play dataset has an error on row[10472] (the row index is without the header) which is the row that contains the “3.0M” under the reviews column which throws that error.

According to the guided project, you are advised to delete the row as the row doesn’t conform with the header of the dataset, check here on instructions on how to delete the row.

After deleting this row, your code should work just fine.

I hope this helps.

1 Like