如何在python数据框中分组命名和合并所有评论?

时间:2019-04-16 02:36:11

标签: python-3.x dataframe concatenation pandas-groupby

df = pd.read_csv('movie_lens')
df1 = df.groupby([['name of movie','reviews']])

##Groupby name of the movie and put all reviews for that movie into one row..#
#name of movie.............reviews#
#titanic...................good#
#titanic...................bad#
#titanic....................great#
#superbad..................funny#
#superbad..................ok#
#superbad..................hilarious#

#How to group movies into one row and concatenate all review it a CSV that's now a dataframe.example#  
#titanic....................good.bad.great#
#superbad....................funny.ok.hilarious#

1 个答案:

答案 0 :(得分:0)

您只是按太多的东西分组。 您想要:

df1 = df.groupby(['name of movie'])['reviews'].apply(list)

或者,稍微简单一点:

df1 = df.groupby('name of movie').reviews.apply(list)

一旦您有list条评论, 随时使用join()或类似方法' '