Pandas: Combine TimeGrouper with another Groupby argument

前端 未结 2 1852
盖世英雄少女心
盖世英雄少女心 2021-01-11 23:21

I have the following DataFrame:

df = pd.DataFrame({
\'Branch\' : \'A A A A A B\'.split(),
\'Buyer\': \'Carl Mark Carl Joe Joe Carl\'.split(),
\'Quantity\': [         


        
2条回答
  •  灰色年华
    2021-01-11 23:42

    From the discussion here: https://github.com/pydata/pandas/issues/3791

    In [38]: df.set_index('Date').groupby(pd.TimeGrouper('6M')).apply(lambda x: x.groupby('Branch').sum())
    Out[38]: 
                       Quantity
               Branch          
    2013-01-31 A              4
    2014-01-31 A             22
               B              3
    

    And a bit more complicated question

    In [55]: def testf(df):
       ....:     if (df['Buyer'] == 'Mark').sum() > 0:
       ....:         return Series(dict(quantity = df['Quantity'].sum(), buyer = 'mark'))
       ....:     return Series(dict(quantity = df['Quantity'].sum()*100, buyer = 'other'))
       ....: 
    
    In [56]: df.set_index('Date').groupby(pd.TimeGrouper('6M')).apply(lambda x: x.groupby('Branch').apply(testf))
    Out[56]: 
                       buyer quantity
               Branch                
    2013-01-31 A        mark        4
    2014-01-31 A       other     2200
               B       other      300
    

提交回复
热议问题