import pandas as pd
# Movie ratings from movielens
df = pd.read_csv('https://raw.githubusercontent.com/warriorkitty/orientlens/master/movielens/ratings.csv')
# Group by movieId and aggregate the mean and the count of each
gb = df.groupby(by=['movieId'], as_index=False).agg({'rating': ['mean', 'count']})
# The mean vote across the whole report
C = float(gb['rating']['mean'].mean())
# The minimum count of votes
m = 100
# Calculate the weighted ratings for each movie
gb['bayes_rating'] = ((gb['rating']['count'] / (gb['rating']['count'] + m )) * gb['rating']['mean'])\
+ (m / ( gb['rating']['count'] + m)) * C
# Show the best movies
gb.sort_values(by='bayes_rating', ascending=False, inplace=True)
# Top 10
gb.head(10)