Python,Pandas Dataframe在groupby之后获取索引

2024-09-19 23:28:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个名为“class_price_df”的熊猫数据帧:

                           email                   cat           class
0               trangthanhtin@yahoo.com     Mobiles & Tablets      1
1                    concomai@yahoo.com     Mobiles & Tablets      4
2                   yenvo.ier@gmail.com     Mobiles & Tablets      2
3                   quyenvy71@yahoo.com     Mobiles & Tablets      4

我按“email”和“cat”分组,以获得“class”的最大值:

^{pr2}$

但结果是:

cat                               Computers & Laptops  Consumer Electronics   
email                                                                         
+coicon7879@gmail.com                               2                     0   
+haiphong82lk@yahoo.com                             0                     2   
+nguyentrungchanhbd@gmai.com                        0                     0   
-abc@gmail.com                                      0                     0   
001kukuku@gmail.com                                 0                     4   
002pnk@gmail.com                                    1                     0   
007.heineken@gmail.com                              4                     0   
007.leson@gmail.com                                 0                     0   

如何找回“索引”并生成类似于以下内容的输出:

                email                 Computers & Laptops        Consumer Electronics
0      +coicon7879@gmail.com                   2                             0  
1      +haiphong82lk@yahoo.com                 0                             2 
2      +nguyentrungchanhbd@gmai.com            0                             0   
3      -abc@gmail.com                          0                             4

Tags: comconsumeremailyahoogmailclasscatelectronics