我想要把这个表格按照酒店名称和入住房型做聚类,去掉评论日期,把评论详情合并到一起。我利用pandas的聚合处理只能算出每个聚类有多少条评论。各位大神有什么方法么?
3条回答 默认 最新
- CSDN专家-HGJ 2021-04-21 21:40关注
给你一个示例代码参考一下,结合自己的数据结构和处理要求作进一步调整。
import pandas as pd df=pd.DataFrame({'hotel':['h1','h1','h3','h2','h3','h1'],'comments':['nice','better services ','not good','wonderful','it\'s ok','friendly people, beautiful place'],'type':['business','single','ordinary','single','business','ordinary']}) df1=df.groupby(['hotel','type'])['comments'].agg(lambda x: '\n'.join(x)) print(df1) #output: hotel type h1 business nice ordinary friendly people, beautiful place single better services h2 single wonderful h3 business it's ok ordinary not good Name: comments, dtype: object
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
- ¥15 如何在scanpy上做差异基因和通路富集?
- ¥20 关于#硬件工程#的问题,请各位专家解答!
- ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
- ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
- ¥30 截图中的mathematics程序转换成matlab
- ¥15 动力学代码报错,维度不匹配
- ¥15 Power query添加列问题
- ¥50 Kubernetes&Fission&Eleasticsearch
- ¥15 報錯:Person is not mapped,如何解決?