Avg_rating is less than what we expected

Screen Link:

My Code:

opened_file = open('AppleStore.csv')
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)
#print(apps_data)
rating_sum = 0
for row in apps_data[1:]:
    rating = float(row[7])
    rating_sum = rating_sum + rating
avg_rating = rating_sum / len(apps_data)
print(len(apps_data))
print(rating_sum)
print(avg_rating)



What I expected to happen:
Pass test

What actually happened:

avg_rating is less than what we expected.

avg_ratingfloat (<class ‘float’>)
(actual) 2.5264656849124756
(expected) 3.526955675976101

Hi @abbiew,

When you calculate the avg_rating, you are dividing by a wrong value of len(apps_data). Instead, you have to find the length of the apps_data dataset excluding the first row, i.e. the head of the dataframe:

avg_rating = rating_sum / len(apps_data[1:])

In this case, the denominator will be equal to the number of values for which you want to calculate the average, so you will get the correct answer.

Hope it was helpful.

3 Likes

Hi @Elena_Kosourova ~
Thank you ~ That did the trick!

1 Like

BTW,
I have had the following:

opened_file = open('AppleStore.csv')
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)

rating_sum = 0
for row in apps_data[1:]:
    rating = float(row[7])
    rating_sum = rating_sum + rating
    
avg_rating = rating_sum / len(apps_data) - 1

and didn’t work, eventhough the exercise suggested answer says:

You can easily get the number of ratings by using len(apps_data). But the first row of apps_data contains the column names, so we’ll need to exclude it — this means that we need to use either len(apps_data[1:]), or len(apps_data) - 1.

Thanks.

1 Like