Dataframe aggregation in groups of 3

I have a DataFrame, like this one:

date open high low close vwap 0 1498907700 0.00010020 0.00010020 0.00009974 0.00010019 0.00009992 1 1498908000 0.00010010 0.00010010 0.00010010 0.00010010 0.00010010 2 1498908300 0.00010010 0.00010010 0.00009957 0.00009957 0.00009992 3 1498908600 0.00009957 0.00009957 0.00009957 0.00009957 0.00000000 4 1498908900 0.00010009 0.00010009 0.00009949 0.00009959 0.00009952 5 1498909200 0.00009987 0.00009991 0.00009956 0.00009956 0.00009974 6 1498909500 0.00009948 0.00009948 0.00009915 0.00009915 0.00009919 ... 789 

And I would like to make an average of every 3 rows and have a new DataFrame, which is 3 times shorter with all sets of 3 rows in the original DataFrame.

+5
source share
1 answer

Use groupby and mean with an array designed to form the groups you need.

 df.groupby(np.arange(len(df)) // 3).mean() 
+7
source

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


All Articles