user1691278
user1691278

Reputation: 1895

Pick the nth element of every group in a grouped dataframe

Suppose I create the following dataframe

using DataFrames
df = DataFrame(A = rand(500), B = repeat(1:10, inner=50), C = 1:500)

and I can do a groupby:

grouped_df = groupby(df,"B")

I will end up with 10 groups. How can I choose, say, the third element of each group, and combine them into a new dataframe? That is, I would like a new dataframe of 10 rows, with each row being the third element of each of the groups?

I looked into combine, but couldn't find a solution. Can I get a hint?

Upvotes: 3

Views: 232

Answers (1)

mbauman
mbauman

Reputation: 31362

To get the third row from every group, groupby first and then combine using indexing:

julia> combine(groupby(df, :B), x->x[3, :])
10×3 DataFrame
 Row │ B      A          C
     │ Int64  Float64    Int64
─────┼─────────────────────────
   1 │     1  0.196572       3
   2 │     2  0.539942      53
   3 │     3  0.243455     103
   4 │     4  0.837491     153
   5 │     5  0.672861     203
   6 │     6  0.0220219    253
   7 │     7  0.303417     303
   8 │     8  0.409596     353
   9 │     9  0.165928     403
  10 │    10  0.752038     453

(I had initially misread the question and suggested logical indexing like df[df.B .== 3, :])

Upvotes: 5

Related Questions