按总和条件分组

Ros*_*ose 2 python group-by loc python-3.x pandas

我有以下df,我想按日期和参考分组,但总和条件.

在这方面,我需要按日期和参考进行分组,并且仅当P>> = PP时才加'Q'列.

df = DataFrame({'Date' : ['1', '1', '1', '1'],
                'Ref' : ['one', 'one', 'two', 'two'],
                'P' : ['50', '65', '30', '38'],
                'PP' : ['63', '63', '32', '32'],
                'Q' : ['10', '15', '20', '10']})

df.groupby(['Date','Ref'])['Q'].sum() #This does the right grouping byt summing the whole column
df.loc[df['P'] >= df['PP'], ('Q')].sum() #this has the right sum condition, but does not divide between Date & Ref
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?提前谢谢了

EdC*_*ica 5

只需在分组之前过滤:

In[15]:
df[df['P'] >= df['PP']].groupby(['Date','Ref'])['Q'].sum()

Out[15]: 
Date  Ref
1     one    15
      two    10
Name: Q, dtype: object
Run Code Online (Sandbox Code Playgroud)

这首先减小了df的大小,因此将加快groupby操作