本文介绍了 pandas :将TimeGrouper与另一个Groupby参数结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下DataFrame:

  df = pd.DataFrame({
'Branch':'AAAAA B'.split(),
'买方':'Carl Mark Carl Joe Joe Carl'.split(),
'Quantity':[1,3,5,8,9,3],
'日期':[
DT.datetime(2013,1,1,13,0),
DT.datetime(2013,1,1,13,5),
DT.datetime(2013,10,1,20,0),
DT.datetime(2013,10,2,10,0),
DT.datetime(2013,12,2,12 ,0),
DT.datetime(2013,12,2,14,0),
]})

from pandas.tseries.resample import TimeGrouper

如何使用TimeGrouper将分组和数据分组为20天?



我以前的所有尝试都失败了,因为我无法将TimeGrouper与groupby函数中的另一个参数组合在一起。



我会非常感谢您的帮助。



谢谢

Andy

解决方案

来自这里的讨论:

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

而且有点复杂的问题

  In [55]:def testf(df):
....:if(df ['Buyer'] =='Mark') .sum()> 0:
.... return系列(dict(quantity = df ['Quantity']。sum(),buyer ='mark'))
....:return Series(dict数量= df ['Quantity']。sum()* 100,buyer ='other'))
....:

在[56]中:df.set_index('Date ').groupby(pd.TimeGrouper('6M'))。apply(lambda x:x.groupby('Branch')。apply(testf))
Out [56]:
买方数量
分行
2013-01-31 A分4
2014-01-31其他2200
B其他300


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': [1,3,5,8,9,3],
'Date' : [
DT.datetime(2013,1,1,13,0),
DT.datetime(2013,1,1,13,5),
DT.datetime(2013,10,1,20,0),
DT.datetime(2013,10,2,10,0),
DT.datetime(2013,12,2,12,0),                                      
DT.datetime(2013,12,2,14,0),
]})

from pandas.tseries.resample import TimeGrouper

How can I group this data by the Branch and on a 20 day period using TimeGrouper?

All my previous attempts failed, because I could not combine TimeGrouper with another argument in the groupby function.

I would deeply appreciate your help.

Thank you

Andy

解决方案

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

这篇关于 pandas :将TimeGrouper与另一个Groupby参数结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 14:40