...

/

Printing Top Rated Movies by Viewer Age

Printing Top Rated Movies by Viewer Age

Learn how to apply a filter on the data read.

How to apply a filter on data?

Let’s compare how the tastes of different age brackets (13–19 vs. 60+) vary. If we want to see all the movies rated by people 60 and older, we’ll execute the code in line 21.

We’ll pass movie_data.age > 60 to itself, and pandas will return a DataFrame that meets this condition:

Press + to interact
import pandas as pd
import matplotlib.pyplot as plt
def movie(nogui=False, movielenspath=''):
user_columns = ['user_id', 'age', 'gender']
users = pd.read_csv(movielenspath + 'movie_lens/u.user', sep='|', names=user_columns, usecols=range(3))
rating_columns = ['user_id', 'movie_id', 'rating']
ratings = pd.read_csv(movielenspath + 'movie_lens/u.data', sep='\t', names=rating_columns, usecols=range(3))
movie_columns = ['movie_id', 'title']
movies = pd.read_csv(movielenspath + 'movie_lens/u.item', sep='|', names=movie_columns, usecols=range(2), encoding="iso-8859-1")
# create one merged DataFrame
movie_ratings = pd.merge(movies, ratings)
movie_data = pd.merge(movie_ratings, users)
# Get the top rated ones by index
elderly = movie_data[(movie_data.age > 60)]
print("Top ten movies for elderly: \n", elderly[:10])
if __name__ == "__main__":
movie()
...