我有以下 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
有没有办法做到这一点?提前谢谢了
蝴蝶刀刀
紫衣仙女
相关分类