首页 > 解决方案 > 如何分组名称并连接python数据框中的所有评论?

问题描述

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#

标签: python-3.xdataframeconcatenationpandas-groupby

解决方案


你只是按太多东西分组。你要:

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

或者,稍微简单一点:

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

一旦你有list评论,请随意join()使用' '或类似的。


推荐阅读