Problem in float statement

I tried running this code but seem to have issues with float?

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

non_games_ratings = []

for row in apps_data:
    apps_data = apps_data[1:]
    rating = float(row[7])
    genre = row[11]
    
    if genre != 'Games':
        non_games_ratings.append(rating)
    
avg_rating_non_games = sum(non_games_ratings) / len(non_games_ratings)
print(non_games_ratings)

What I expected to happen: float clause would convert the string of rating.

What actually happened:

ValueError: could not convert string to float: 'user_rating'

Hello @thomas.palesky, welcome to the community!

The error is here:

This line of code apps_data = apps_data[1:] should be before the for loop as in:

apps_data = apps_data[1:]
for row in apps_data:
    rating = float(row[7])
    genre = row[11]

This way, the header column is removed from the dataset before converting the values in the user_rating column to float.

Happy learning!

1 Like