Skip to content

Instantly share code, notes, and snippets.

@LouisdeBruijn
Last active August 28, 2019 12:03
Show Gist options
  • Save LouisdeBruijn/20c875cf6e80587b742f8e39ff070705 to your computer and use it in GitHub Desktop.
Save LouisdeBruijn/20c875cf6e80587b742f8e39ff070705 to your computer and use it in GitHub Desktop.
mappings for users and movies
def map_movies(movie_ids):
'''takes a list of movie_ids and returns a list of dictionaries with movies information'''
df = pd.read_csv('ml-1m/movies.dat', delimiter='::', header=None,
names=['movie_id', 'title', 'genre'], engine='python')
# add years to a new column 'year' and remove them from the movie title
df['year'] = df['title'].str[-5:-1]
df['title'] = df['title'].str[:-6]
# creates an ordered list of dictionaries with the movie information for all movie_ids
mapped_movies = [df[df['movie_id'] == i].to_dict('records')[0] for i in movie_ids]
return mapped_movies
def map_users(user_ids):
'''takes a list of user_ids and returns a list of dictionaries with user information'''
df = pd.read_csv('ml-1m/users.dat', delimiter='::', header=None,
names=['user_id', 'gender', 'agerange', 'occupation', 'timestamp'], engine='python')
df = df.drop(['timestamp'], axis=1)
mapped_users = [df[df['user_id'] == i].to_dict('records')[0] for i in user_ids]
return mapped_users
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment