Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aggregating Dataframe 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 would like to do a mean of each 3 rows and have a new DataFrame which is then 3 times shorter with the mean of all sets of 3 rows inside the source DataFrame.

like image 394
Maxime B Avatar asked Jul 03 '17 23:07

Maxime B


People also ask

How do you aggregate by a group?

The Syntax for Using Group By in SQLORDER BY column_name; The columns to be retrieved are specified in the SELECT statement and separated by commas. Any of the aggregate functions can be used on one or more than one of the columns being retrieved.

How do you aggregate a data frame?

Pandas DataFrame aggregate() MethodThe aggregate() method allows you to apply a function or a list of function names to be executed along one of the axis of the DataFrame, default 0, which is the index (row) axis. Note: the agg() method is an alias of the aggregate() method.


1 Answers

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

df.groupby(np.arange(len(df)) // 3).mean()
like image 80
piRSquared Avatar answered Nov 08 '22 14:11

piRSquared