Allow and perform operations on each 3 rows of the DataFrame

I have this DataFrame and I would like to provide slices of three rows, returning a new DateFrame with all these three sets of rows combined with one containing the highest date, lowest open, highest maximum and last close.

date open high low close 0 1498907700 0.00010020 0.00010020 0.00009974 0.00010019 1 1498908000 0.00010010 0.00010010 0.00010010 0.00010010 2 1498908300 0.00010010 0.00010010 0.00009957 0.00009957 3 1498908600 0.00009957 0.00009957 0.00009957 0.00009957 4 1498908900 0.00010009 0.00010009 0.00009949 0.00009959 5 1498909200 0.00009987 0.00009991 0.00009956 0.00009956 ... 789 

Can I make a groupby look and apply a function on all of these slices to get a new DataSet?

The output DataSet should look like this:

  date open high low close 0 1498908300 0.00010010 0.00010020 0.00009957 0.00009957 1 1498909200 0.00009957 0.00010009 0.00009949 0.00009956 ... 789 

If the number of lines is not a multiple of 3, I would like the orphans to be processed in the first set of 3 lines.

+1
source share
1 answer

You can use groupby and aggregate

 df.groupby(df.index//3).agg({'date': 'max', 'open': 'min', 'high': 'max', 'low': 'min','close': 'last'}) 

You are getting

  date open high low close 0 1498908300 0.00010010 0.00010020 0.00009957 0.00009957 1 1498909200 0.00009957 0.00010009 0.00009949 0.00009956 
+1
source

Source: https://habr.com/ru/post/1269459/


All Articles