1

For each cent, I want to add together the volumes.

So in this data set, all trades at prices between 188.415-188.42 would get their volumes added together, all 188.43 trades added together, etc. I'm currently using pandas to manage the data, and I'm unsure of what functions I could accomplish this with.

Example Data:

Time|Volume|Price
09:30:00|200|188.42
09:30:00|500|188.41
09:30:00|100|188.415
09:30:00|100|188.41
09:30:00|590|188.42
09:30:00|100|188.415
09:30:00|100|188.4
09:30:00|200|188.42
09:30:00|900|188.41
09:30:00|249|188.42
09:30:00|100|188.41
09:30:00|300|188.415
09:30:00|300|188.42
09:30:00|100|188.43
09:30:00|100|188.44
09:30:00|900|188.43
09:30:00|200|188.42
09:30:00|100|188.43
09:30:00|100|188.42
09:30:00|500|188.43
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Samuel
  • 65
  • 1
  • 5

1 Answers1

3

You could round the Price column, store them in a (temporary) approx column, then perform a groupby/agg operation:

df['approx'] = df['Price'].round(2)
df.groupby('approx')['Volume'].sum()

yields

# approx
# 188.40     100
# 188.41    1600
# 188.42    2339
# 188.43    1600
# 188.44     100
# Name: Volume, dtype: int64

Alternatively, you could forego the approx column and supply the values to df.groupby directly:

In [142]: df.groupby(df['Price'].round(2))['Volume'].sum()
Out[142]: 
Price
188.40     100
188.41    1600
188.42    2339
188.43    1600
188.44     100
Name: Volume, dtype: int64
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677