Python,重新组织Pandas DataFrame中的列

时间:2014-12-01 08:44:59

标签: python pandas indexing group-by

我有一个名为'class_price_df'的Pandas DataFrame:

                            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

我按“电子邮件”和“猫”分组,以获得“课堂”的最大值:

class_price_df = class_price_df.groupby(['email','cat']).max().unstack('cat').fillna(0)

输出结果为:

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  

如何获得类似于

的输出
                 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

我试过

class_price_df.reset_index(inplace=True, level='class')

但输出是:

                            email                 class           \
cat                                       Automotive & Gadgets  Cameras   
0                +841869087649@gmail.com                     0        0   
1                  +coicon7879@gmail.com                     0        0   
2                +haiphong82lk@yahoo.com                     0        0   
3           +nguyentrungchanhbd@gmai.com                     0        0 

0 个答案:

没有答案