# Need help finishing a groupby random sampling function

**URL:** <https://discourse.julialang.org/t/need-help-finishing-a-groupby-random-sampling-function/38485>\
**Category:** General Usage\
**Created:** [April 30, 2020, 1:58pm UTC](https://discourse.julialang.org/t/need-help-finishing-a-groupby-random-sampling-function/38485 "2020-04-30T13:58:41Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Julia1](https://avatars.discourse-cdn.com/v4/letter/j/db5fbb/32.png) [@Julia1](https://discourse.julialang.org/u/Julia1)\
**Post date:** [April 30, 2020, 1:58pm UTC](https://discourse.julialang.org/t/need-help-finishing-a-groupby-random-sampling-function/38485/1 "2020-04-30T13:58:41Z")

</div>

Hi how’s it going, I have a function that takes in a dataframe, groups by a column, and takes a random sample of that column. I want to set some condition that says “If the size of this subdataframe is less than 20, don’t return anything, otherwise, return the random sample”

heres the code:

```
new_df = by(df,:group) do sdf
    sample_df = sdf[StatsBase.sample(axes(sdf, 1), min(size(sdf,1),20); replace = false, ordered = true), :]

    #here is where I need the condition that says "if you've got less than 20 rows, I don't need you in my returned dataframe"

return sample_df
end

return new_df

```

Thank you for your help as always!
