0

I have some trade data as seen in the image. Data example There can be many entries for a single instant in time. I would like to do some calculations based on PRICE and SIZE at each moment in time. I would like to do something like:

total_size = eur_jpy_order.groupby(['TIMESTAMP', 'BUY_SELL_FLAG']).sum()
avg_price = eur_jpy_order.groupby(['TIMESTAMP', 'BUY_SELL_FLAG']).mean()

and then take just have a dataframe with TIMESTAMP, BUY_SELL_FLAG, total_size and avg_price, with a single entry for each time step. I can't find a simple way of doing this. Could anyone advise?

havingaball
  • 378
  • 2
  • 11

1 Answers1

1

I think you can use agg:

>>> eur_jpy_order

           BUY_SELL_FLAG  PRICE  SIZE
TIMESTAMP
1                      1     10    10
1                      1     20    20
1                      1     30    30
1                      2     40    40

>>> eur_jpy_order.groupby(['TIMESTAMP', 'BUY_SELL_FLAG']) \
                 .agg(total_size=('SIZE', 'sum'),
                      avg_price=('PRICE', 'mean'))

                         total_size  avg_price
TIMESTAMP BUY_SELL_FLAG
1         1                      60       20.0
          2                      40       40.0
Corralien
  • 109,409
  • 8
  • 28
  • 52