0

friends. i try to group my data from csv with pandas. i have next csv table: ames=('datetime', 'operation', 'price', 'count')

11.04.2017 11:45    BUY 1,06475 1
11.04.2017 11:45    BUY 1,0648  1
11.04.2017 11:45    BUY 1,06485 2
11.04.2017 11:46    BUY 1,0649  18
11.04.2017 11:46    SELL    1,06485 2
11.04.2017 11:46    BUY 1,06485 1
11.04.2017 11:46    SELL    1,06485 4
11.04.2017 11:46    SELL    1,06485 1
11.04.2017 11:50    SELL    1,0646  1
11.04.2017 11:50    SELL    1,0646  1
11.04.2017 11:50    SELL    1,0646  1
11.04.2017 11:51    SELL    1,0646  2
11.04.2017 11:51    SELL    1,0646  5
11.04.2017 11:51    BUY 1,0646  3
11.04.2017 11:51    SELL    1,0646  8
11.04.2017 11:51    BUY 1,06465 2
11.04.2017 11:51    SELL    1,06465 3
11.04.2017 11:51    SELL    1,06465 1
11.04.2017 11:51    SELL    1,06465 1

and i need to group data with 5 min period and price like this:

11.04.2017 11:45        SELL    BUY
        1,06455 26  1
        1,0646  3   19
        1,06465 43  23
        1,0647  15  13
        1,06475 12  21
        1,0648  45  26
        1,06485 27  24
        1,0649  0   29

    11.04.2017 11:50    1,0646  34  31
        1,06465 26  32
        1,0647  0   3

can anybody help me?

Serhii
  • 11
  • 1
  • 4
    Possible duplicate of [Pandas group by time windows](http://stackoverflow.com/questions/22769047/pandas-group-by-time-windows) – tmrlvi Apr 14 '17 at 07:39

1 Answers1

0

IIUC you can do something similar to the foloowing:

In [26]: df.groupby([pd.Grouper(key='datetime', freq='5T'), 'operation']) \
           .agg({'price':'mean', 'count':'sum'}) \
           .unstack('operation')
Out[26]:
                        price           count
operation                 BUY      SELL   BUY SELL
datetime
2017-11-04 11:45:00  1.064830  1.064850    23    7
2017-11-04 11:50:00  1.064625  1.064617     5   23
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419