首页 > 解决方案 > 如何在 Python Pandas 中将多行更改为单行

问题描述

我的数据框看起来像:

  item_id   item_name   item_desc  rating item_type     price
0     it1      laptop                 6.0      asus   50000.0
1     it1      laptop                 7.0      acer   30000.0
2     it1      laptop                 3.0        hp   20000.0
3     it2  smartphone                 6.0    iphone  600000.0
4     it2  smartphone                 5.0   samsung  750000.0
5     it1      laptop                 4.0      dell   10000.0
6     it3       radio  Best radio     8.0      tech   30000.0
7     it3       radio  Best radio     9.0      tech   30000.0

我想改变它们的样子:

  item_id   item_name   item_desc  average_rating               item_type  \
0     it1      laptop                         4.0  [asus, acer, hp, dell]   
1     it2  smartphone                         5.5       [iphone, samsung]   
2     it3       radio  Best radio             8.5                  [tech]   

                          price  
0  [50000, 30000, 20000, 10000]  
1              [600000, 750000]  
2                       [30000]  

从上面,我想将列 rating 中的多个数据更改为 average_rating,将 item_type 和 price 中的多个数据转换为列表。

你能帮我看看python pandas中的脚本程序怎么样?

标签: pythonpandas

解决方案


推荐阅读