Screen 5: Guided Project: Profitable App Profiles for the App Store and Google Play Markets

Screen Link:

My Code:

android_clean = []
already_added = []

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

print(len(android_clean))

What I expected to happen:
It should give me the following number of rows:
Number of rows: 9659
Number of columns: 13

What actually happened:
But I only got 1 row:


Number of rows: 1
Number of columns: 13

What’s missing here? I can’t seem to find the error even if i run it with the solution code. Appreciate your help!

Hi @miah

Can you share the code where you have added the app names as keys in the reviews_max dictionary and n_reviews as values?
Better yet, could you please share your notebook with the community to help identify what could be wrong here.
I am asking this because your code is very different from what was instructed in GP content. Thanks.

1 Like

Hi Rucha,

Thanks for your reply!

Here’s the notebook! Apologies if it’s a bit messy as I’m still trying to organize the notebook. Appreciate your help on this!

Guided Project - Apps Profile.ipynb (9.6 KB)

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

Hi @miah

Took me a while to figure out the problematic code!

#dictionary code to remove duplicate entries
#my code
review_max = {}

for apps in android:
    name = apps[0]

    """
    This is where the problem is! you have used `app` instead of 
    `apps` in this code.*
    """
    n_reviews = float(app[3])

    if name in review_max and review_max[name] < n_reviews:
        review_max[name]=n_reviews

    elif name not in review_max:
        review_max[name] = n_reviews

(*) The app variable was created in the previous loop and hence stores the last record of the android application list.
Due to this the review_max dictionary only had one rating (value) for all the application names (keys).
The solution code uses a different dictionary for the same step, hence that dictionary would have given you correct results for clean records.

To test this, insert a code cell below the code cell [26] in the notebook attached, and observe the results for the review_max dictionary.
In a separate cell run the solution and review the results of the reviews_max dictionary. You should be able to understand the difference.

2 Likes

Hi @Rucha,

Thanks so much for helping me troubleshooting this! I should have paid more attention :frowning:

Following up on this, can I ask why the code still runs even with app and not apps?

"""
This is where the problem is! you have used `app` instead of 
`apps` in this code.*
"""
n_reviews = float(app[3])

if name in review_max and review_max[name] < n_reviews:
    review_max[name]=n_reviews

elif name not in review_max:
    review_max[name] = n_reviews

Why doesn’t it prompt an error from this cell? In different word, since “app” is not defined anywhere, how can it still run?

Thanks for your help again!

Hi @miah

app variable is defined in the previous for loop. The loop executes as many times as the length of the android dataset. It’s just that, outside that for loop, the variable app takes the last row value.

The variable exists till the time you flush it out. Hence it will not give you an error. Try deleting the variable between these two code cells and execute your code with app instead of apps:

Basically,

Code where `app`  was used

del app

Code where `apps` should have been used but you used `app`

You should get a NameError!

1 Like

Hi @Rucha - I understand now - thank you so much for your help!