Julia: Sample N groups from a grouped dataframe

An alternative is to use sample from StatsBase.jl:

@pipe df |>
      groupby(_, :item) |>
      _[sample(1:length(_), 2, replace=false)] |>

if you accept a random fraction q from your DataFrame (not a fixed number) then it is even easier:

@pipe df |>
      groupby(_, :item) |>
      combine(sdf -> rand() < q ? sdf : DataFrame(), _)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top