如何对GroupBy做其他功能没有任何意义?

时间:2016-07-06 14:49:49

标签: python pandas dataframe group-by

我正在使用python pandas DataFrames,我想按类别对我的数据进行分组,我不想要其他功能的任何均值或中位数(PriceBucket,success_rate和products_by_number)。我的DataFrame看起来像这样:

   PriceBucket  success_rate    products_by_number  category
0          0    6.890                      149837   10
1          1    7.240                      105447   10
2          2    7.710                      145295   10
3          3    8.090                      181323   10
4          4    8.930                       57187   10
5          5    8.110                      133449   10
6          6    7.920                      142858   10
7          7    8.230                      115109   10
8          8    8.510                      121930   10
9          9    8.340                      122510   10
10         0    10.520                      28105   20
11         1    9.770                       27494   20
12         2    10.080                      26758   20
13         3    10.180                      29973   20
14         4    9.860                       29175   20
15         5    9.950                       23807   20
16         6    9.550                       30520   20
17         7    9.550                       23653   20
18         8    8.990                       27514   20
19         9    6.710                       26152   20
20         0    11.060                      39538   60
21         1    10.740                      34479   60
22         2    10.700                      36133   60
23         3    10.900                      34220   60
24         4    11.290                      46001   60
25         5    11.130                      26705   60
26         6    11.040                      37258   60
27         7    11.150                      34561   60
28         8    10.845                      35495   60
29         9    10.220                      35434   60
30         0    8.380                       34134   90
31         1    7.920                       32160   90
32         2    8.170                       29500   90
33         3    8.270                       31688   90
34         4    8.395                       38977   90
35         5    8.620                       27130   90
36         6    8.440                       31007   90
37         7    8.570                       31005   90
38         8    8.170                       32659   90
39         9    7.290                       30227   90

这正是我想要的:

           PriceBucket  success_rate    products_by_number
category
 10         0           6.890                      149837   
            1           7.240                      105447   
            2           7.710                      145295
            3           8.090                      181323
            4           8.930                       57187
            5           8.110                      133449
            6           7.920                      142858
            7           8.230                      115109
            8           8.510                      121930
            9           8.340                      122510
 20         0          10.520                       28105
            1           9.770                       27494
            2          10.080                       26758
            3          10.180                       29973
            4           9.860                       29175
            5           9.950                       23807
            6           9.550                       30520
            7           9.550                       23653
            8           8.990                       27514
            9           6.710                       26152
 60         0          11.060                       39538
            1          10.740                       34479
            2          10.700                       36133
            3          10.900                       34220
            4          11.290                       46001
            5          11.130                       26705
            6          11.040                       37258
            7          11.150                       34561
            8          10.845                       35495
            9          10.220                       35434
 90         0           8.380                       34134
            1           7.920                       32160
            2           8.170                       29500
            3           8.270                       31688
            4           8.395                       38977
            5           8.620                       27130
            6           8.440                       31007
            7           8.570                       31005
            8           8.170                       32659
            9           7.290                       30227

怎么办?非常感谢

1 个答案:

答案 0 :(得分:1)

假设您的数据帧为df,那么您需要:

print df.set_index(['category', 'PriceBucket'])

                      success_rate  products_by_number
category PriceBucket                                  
10       0                   6.890              149837
         1                   7.240              105447
         2                   7.710              145295
         3                   8.090              181323
         4                   8.930               57187
         5                   8.110              133449
         6                   7.920              142858
         7                   8.230              115109
         8                   8.510              121930
         9                   8.340              122510
20       0                  10.520               28105
         1                   9.770               27494
         2                  10.080               26758
         3                  10.180               29973
         4                   9.860               29175
         5                   9.950               23807
         6                   9.550               30520
         7                   9.550               23653
         8                   8.990               27514
         9                   6.710               26152
60       0                  11.060               39538
         1                  10.740               34479
         2                  10.700               36133
         3                  10.900               34220
         4                  11.290               46001
         5                  11.130               26705
         6                  11.040               37258
         7                  11.150               34561
         8                  10.845               35495
         9                  10.220               35434
90       0                   8.380               34134
         1                   7.920               32160
         2                   8.170               29500
         3                   8.270               31688
         4                   8.395               38977
         5                   8.620               27130
         6                   8.440               31007
         7                   8.570               31005
         8                   8.170               32659
         9                   7.290               30227