Pandas Groupby多列(Timeseries,String Column)失败

时间:2017-07-21 20:43:19

标签: python pandas pandas-groupby

我有一个TimeSeries数据,我试图按月分组,然后是服务类型,所以基本上分组在多个列上。

我可以让每个小组独立工作(见下面的两个案例)。但是,当我尝试将它们组合在一起时失败并出现以下异常(下面)

第一个groupby就在'service'列上:

import xlwings as xw


bk = xw.Book(file_path)
sheet = bk.sheets['Sheet1'] # or whatever the sheet is named

def xl_col_sort(sheet,col_num):
    sheet.range((2,col_num)).api.Sort(Key1=sheet.range((2,col_num)).api, Order1=1)
return

第二个groupby在TimeSeries Index上(频率= Month):

df_service =df_mem[['service','amount']].groupby('service').agg(['sum','count'])


                                                   sum count
service                                                     
10-Class Pack - $170+HST                       1728.90     9
646 Blue T-shirt (stars)                         25.00     1
646 Foundations + 12 classes $210+HST           237.30     1
646 Foundations + 8 Classes - $159+HST          876.38     5
646 Klawkov Tee, Wonder Woman Tee, 2 Drop-Ins    96.05     1
Bronze (8/mth)                                 1830.60    12
Bronze (8/mth) $135+HST                        1121.07    10
Clothing - Sweatpants XL, Grey Hoodie L          94.27     2
Drop-In $20+HST                                 158.20     7
Gold (Unlimited) - $185+HST                    1604.56    19
Leather lifting straps                           25.00     1
Men's Dimas Tee, Large                           28.25     1
Open Gym                                        220.35     3
Open Gym - $65+HST                               83.07     3
Red 646 Raglan, Large                            33.90     1
Silver                                         2237.40    12
Silver (12/mth) $165+HST                       1294.28    13
Test                                              2.00     2
Thumb Tape                                        6.25     1
Unlimited Gold                                 6898.65    33
Women's Fleece Pants                             48.59     1
Wonder Woman muscle tank (2) +HST                56.50     1

当我将两者结合在一起时,它失败了:

df_montly_service = df_mem[['service','amount']].groupby(pd.Grouper(freq='M')).agg(['sum','count'])
             amount      
                sum count
date                     
2017-03-31     2.00     2
2017-04-30  3053.68    38
2017-05-31  4854.03    34
2017-06-30  5650.85    39
2017-07-31  5227.17    29

0 个答案:

没有答案