我有以下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
有没有办法做到这一点?提前谢谢了