0

我正在使用具有以下结构的 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 做到这一点?

4

1 回答 1

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
于 2019-02-03T19:42:29.007 回答