Martijn
Martijn

Reputation: 27

Calculate mean of specific rows Julia dataframe

I am new to Julia and I have a relatively simple question (I think) about calculating means in Julia dataframes.

Imagine I have the following dataframe:

df = DataFrame(A=1:12, B=13:24) 

Where my goal is to calculate the mean values in both column A and B for every 4 rows.

So in column A that includes 3 means for the values [1,2,3,4], [5,6,7,8],[9,10,11,12]. And for column B includes 3 means for the values [13,14,15,16],[17,18,19,20],[21,22,23,24].

I tried splitting the dataframe into 3 dataframes manually, but I have a large dataset so iterating over rows will be much more efficient.

Hopefully someone can help me out (I am using Julia version 1.0.3).

Upvotes: 1

Views: 1958

Answers (1)

Michael K. Borregaard
Michael K. Borregaard

Reputation: 8044

See the documentation here: https://dataframes.juliadata.org/stable/man/split_apply_combine/ So you could do

df.group = repeat(1:3, inner = 4)
aggregate(df, :group, mean)

Upvotes: 4

Related Questions