[英]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 我正在处理movielens数据集,我想通过应用一个函数创建一个新列,其基本思路是:1)从ratings_dataframe获取相应的电影id 2)使用此电影ID从moviedata_frame中查找电影的名称3)将此值复制到评级数据框中的相应单元格
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') 错误是:('无效类型比较',您在索引movie_id处发生')
You don't need a function for this simple mapping: 您不需要此简单映射的函数:
ratings_dataframe['title'] = \
ratings_dataframe['movie_id'].map(movies.set_index('movie_id')['title'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.