python-3.x - 如何分组名称并连接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#
解决方案
你只是按太多东西分组。你要:
df1 = df.groupby(['name of movie'])['reviews'].apply(list)
或者,稍微简单一点:
df1 = df.groupby('name of movie').reviews.apply(list)
一旦你有list
评论,请随意join()
使用' '
或类似的。
推荐阅读
- azure-sqldw - 可以使用什么工具将 SQL Server DB/DW 迁移到 Azure Synapse(以前称为 Azure SQL DW)?
- javascript - TypeError:无法读取未定义错误的属性“样式”
- reactjs - 如何在reactJS中控制台记录当前图像的索引
- tomcat - Tomcat 抱怨 .schedule() 中的参数类型
- mysql - clickhouse-mysql 出现错误:ImportError: cannot import name 'charset_to_encoding' from 'pymysql.charset'
- android - 如何使用没有安全漏洞的 AWS S3 凭证?
- protobuf-net - protobuf.net v2.4.6 有效,V3.0.29 抛出“一旦生成序列化程序就无法更改类型”
- amazon-web-services - 使用 Glue 从 S3 存储桶转换 CSV 文件并将转换后的数据保存回另一个 S3 存储桶
- c# - 如何使 SignalR 服务器仅对特定 URL 使用长轮询?
- r - 带条件变量的交叉表