Reputation: 23
In dataframe I have columns such as prime_genre and user_rating. I want to calculate average number of ratings (to do that i try to summarize ratings for each app and then divide by number of apps occurrences). My solution looks like this:
total=0
len_genre=0
for genre in prime_genre:
for i in iosfree['prime_genre']:
if(i == genre):
len_genre += 1
total += iosfree.loc[iosfree['prime_genre']==i, 'user_rating']
print("Genre: ", genre, "Average rating: ", total/len_genre)
Result looks like this:
Genre: Music Average rating: 0 NaN
5 NaN
43 NaN
48 NaN
51 NaN
Average rating shouldn't be zero. Could you please give me some advice how to fix it? I am new to Python.
Upvotes: 0
Views: 870
Reputation: 323366
Sounds like you can do with groupby
with agg
df.groupby('prime_genre')['user_rating'].agg(['count','mean'])
Upvotes: 1