Gru
Gru

Reputation: 965

Applying a function to a column in pandas

I am working on the movielens dataset, I would like to create a new column by applying a function , the basic idea is : 1) get corresponding movie id from the ratings_dataframe 2)use this movie id to find the name of the movie from moviesdata_frame 3)and copy this value to the corresponding cell in the ratings dataframe

My code consists of:

def getname(p):
    nm =  movies.loc[movies['movie_id'] == 'p']['title']
    return nm   



ratings['title'] = ratings.apply(lambda row:getname(gg['movie_id']))

The error is :('invalid type comparison', u'occurred at index movie_id')

Upvotes: 0

Views: 64

Answers (1)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210812

You don't need a function for this simple mapping:

ratings_dataframe['title'] = \
    ratings_dataframe['movie_id'].map(movies.set_index('movie_id')['title'])

Upvotes: 1

Related Questions