0

I am working with a df with the following structure:

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'],
            })   

I have managed to get the weighted percentage that represents my Quantity out of MarketQuantity, when Price is above Mkt Price (and showing it by Date and Ref)

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

However, when I am trying to group it only by Date I get:

Date  Output 
1     0.4

which is the sum of previous outputs, when it should be 0.14 (30+40)/(100+400).

How can I do that with groupby?

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Rose
  • 203
  • 2
  • 10

1 Answers1

1

IIUC, may be something like this:

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
anky
  • 74,114
  • 11
  • 41
  • 70