# Avg_rating is less than what we expected

My Code:

``````opened_file = open('AppleStore.csv')
#print(apps_data)
rating_sum = 0
for row in apps_data[1:]:
rating = float(row)
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.

3 Likes

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

1 Like

BTW,

``````opened_file = open('AppleStore.csv')

rating_sum = 0
for row in apps_data[1:]:
rating = float(row)
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