Skip to content

Instantly share code, notes, and snippets.

@rohitdholakia
Created December 24, 2011 21:02
Show Gist options
  • Save rohitdholakia/1518336 to your computer and use it in GitHub Desktop.
Save rohitdholakia/1518336 to your computer and use it in GitHub Desktop.
A python script to find average ratings for all movies
import sys,os
import MySQLdb
def connect():
conn = MySQLdb.connect(host = "localhost",user = "root",db = "netflix")
cursor=conn.cursor()
return cursor
def getAverage(cursor,id):
#Take a movieId and find out the average rating for that movie
cursor.execute("select avg(rating) from allRatings where movieId="+id)
return cursor.fetchone()
def getRatings(cursor,id):
cursor.execute("select count(*) from allRatings where movieId="+id)
cursor = connect()
forMovies = open(sys.argv[1],'w')
for i in range(1,17771):
avg = getAverage(cursor,i)
number = getRatings(cursor,i)
forMovies.write(i+","+avg+","+number+"\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment