Skip to main content
 首页 » 编程设计

python之Pandas dataframe : groupby one column, 但由其他人连接和聚合

2025年05月04日20kerrycode

<分区>

如何转换以下输入数据(从 Excel 文件提供的 Pandas 数据框):

ID      Category                    Speaker     Price 
334014  Real Estate Perspectives    Tom Smith   100 
334014  E&E                         Tom Smith   200 
334014  Real Estate Perspectives    Janet Brown 100 
334014  E&E                         Janet Brown 200 

进入这个:

ID      Category                    Speaker                 Price 
334014  Real Estate Perspectives    Tom Smith, Janet Brown  100 
334014  E&E                         Tom Smith, Janet Brown  200    

所以基本上我想按类别分组,连接演讲者,而不是汇总价格。

我尝试了 Pandas dataframe.groupby() 和 .agg() 的不同方法,但无济于事。也许有更简单的纯 Python 解决方案?