【问题标题】:Group By with sumproduct使用 sumproduct 分组
【发布时间】:2019-06-27 15:08:59
【问题描述】:

我正在使用具有以下结构的 df:

df = DataFrame({'Date' : ['1', '1', '1', '1'],
            'Ref' : ['one', 'one', 'two', 'two'],
            'Price' : ['50', '65', '30', '35'],
            'MktPrice' : ['63', '63', '32', '32'],
            'Quantity' : ['10', '15', '20', '10'],
            'MarketQuantity': ['50', '50', '100', '100'],
            'Weightings' : ['2', '2', '4', '4'],
            'QxWeightings' : ['20', '30', '80', '40'],
            'MktQxWeightings': ['100', '100', '400', '400'],
            })   

当价格高于 Mkt 价格(并按日期和参考显示)时,我已设法从 MarketQuantity 中获得代表我的数量的加权百分比

def percentage(x):
    return (x.loc[x['Price'] >= x['MktPrice'], ['QxWeightings']].sum()/(x['MktQxWeightings'].sum()/len(x)))

df.groupby(['Date', 'Ref']).apply(percentage)

Date  Ref   Output 
1     one   0.3
1     two   0.1

但是,当我尝试仅按日期分组时:

Date  Output 
1     0.4

这是之前输出的总和,它应该是 0.14 (30+40)/(100+400)。

如何使用 groupby 做到这一点?

【问题讨论】:

    标签: python pandas group-by apply pandas-loc


    【解决方案1】:

    IIUC,可能是这样的:

    def percentage(x):
        return (x.loc[x['Price'] >= x['MktPrice'], ['QxWeightings']].sum()/(x['MktQxWeightings'].sum()/len(x)))
    
    df_new=df.groupby(['Date', 'Ref','MktQxWeightings']).apply(percentage).reset_index()
    print(df_new)
    
      Date  Ref  MktQxWeightings  QxWeightings
    0    1  one              100           0.3
    1    1  two              400           0.1
    
    df_new.groupby('Date')['MktQxWeightings','QxWeightings'].apply(lambda x: x['QxWeightings'].\
                                                               cumsum().sum()*100/x['MktQxWeightings'].sum())
    
    Date
    1    0.14
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 2022-01-24
      • 1970-01-01
      • 2018-03-16
      • 1970-01-01
      相关资源
      最近更新 更多