BLACK FRIDAY EXTRA SAVINGS EVENT - EXTENDED
START FREE

Guided Project: Profitable App Profiles error str vs float

Removing Duplicate Entries: Part Two - Error

My Code:

reviews_max = {}
for app in dataset_google[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] = name

What actually happened:

TypeError                                 Traceback (most recent call last)
<ipython-input-34-672b507d638c> in <module>
      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
      7     elif name not in reviews_max:

TypeError: '<' not supported between instances of 'str' and 'float'

I am not sure how to fix this error as I already read similar topics for this problem and I checked - I did delete the row in the previous steps and I have the correct\expected number of rows and columns in the data set. (I am referring to the row 10472 which corresponded to the app Life Made WI-Fi Touchscreen Photo Frame)

If the data is not “clean” somewhere else what would be the method to find out so I can proceed?

As the error points out -

The code is trying to compare a string to a float. That’s not a valid operation, hence the error.

The float, in this case, would be n_reviews, since you define it as float(app[3]).

That means, reviews_max[name] is returning a string somewhere.

Now, you are creating and populating the reviews_max dictionary above. So, that means, somewhere in the code you are assigning a string value to a key.

Try to go through it again and see where there might be happening and if that matches with the requirements. Feel free to ask more questions if you get stuck.

Thank you. I figured it out.
This line of code was causing a mistake and as far as I can see it has nothing to do with the TypeError

reviews_max[name] = name

and it should have been:

 reviews_max[name] = n_reviews

This is not the part where the comparison was happening, this is assignment, so not sure why this specific TypeError was generated, but still problem solved.

You were storing strings in the dictionary reviews_max. name is a string.

That’s why you got the error at the comparison later because you had assigned a string as a value for that key in the dictionary.

I would also recommend focusing more on why such errors occur and trying to be sure about how you ended up solving them before moving forward. Debugging code is an important/good skill to develop.