# How to sample a Data frame

**URL:** https://discourse.julialang.org/t/how-to-sample-a-data-frame/32791
**Category:** General Usage
**Created:** [December 30, 2019, 10:06am UTC](https://discourse.julialang.org/t/how-to-sample-a-data-frame/32791 "2019-12-30T10:06:21Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![imrankhan\_juliaLang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/imrankhan_julialang/32/12020_2.png) [@imrankhan\_juliaLang](https://discourse.julialang.org/u/imrankhan_juliaLang)
#### Post date: [December 30, 2019, 10:06am UTC](https://discourse.julialang.org/t/how-to-sample-a-data-frame/32791/1 "2019-12-30T10:06:21Z")

</div>

How can i sampling Data Frame?  
**Like python data.sample() method**  
**NB: replace must need**

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [December 30, 2019, 10:18am UTC](https://discourse.julialang.org/t/how-to-sample-a-data-frame/32791/2 "2019-12-30T10:18:59Z")

</div>

Just can just use random row indices like:

```julia
julia> using DataFrames, Random

julia> df = DataFrame(a = 1:10, b = rand(10))
10×2 DataFrame
│ Row │ a │ b │
│ │ Int64 │ Float64 │
├─────┼───────┼──────────┤
│ 1 │ 1 │ 0.180922 │
│ 2 │ 2 │ 0.726072 │
│ 3 │ 3 │ 0.802304 │
│ 4 │ 4 │ 0.769662 │
│ 5 │ 5 │ 0.705299 │
│ 6 │ 6 │ 0.266686 │
│ 7 │ 7 │ 0.332831 │
│ 8 │ 8 │ 0.393075 │
│ 9 │ 9 │ 0.1936 │
│ 10 │ 10 │ 0.830922 │

julia> df[shuffle(1:nrow(df))[1:5], :]
5×2 DataFrame
│ Row │ a │ b │
│ │ Int64 │ Float64 │
├─────┼───────┼──────────┤
│ 1 │ 7 │ 0.332831 │
│ 2 │ 8 │ 0.393075 │
│ 3 │ 1 │ 0.180922 │
│ 4 │ 5 │ 0.705299 │
│ 5 │ 9 │ 0.1936 │

```

The `shuffle` function returns a random ordering of the range from 1 to the number of rows of your dataframe, which you can then index with `[1:x]` where x is the number of samples you want.

Alternatively, there are ML/stats packages that implement their own way of splitting data into train and test data, like MLJ or Turing - check their docs if that’s of interest.

---

<div class="post-metadata">

### Author: ![imrankhan\_juliaLang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/imrankhan_julialang/32/12020_2.png) [@imrankhan\_juliaLang](https://discourse.julialang.org/u/imrankhan_juliaLang)
#### Post date: [December 30, 2019, 10:27am UTC](https://discourse.julialang.org/t/how-to-sample-a-data-frame/32791/3 "2019-12-30T10:27:30Z")

</div>

need 100 rows data to 1000 sample

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [December 30, 2019, 10:30am UTC](https://discourse.julialang.org/t/how-to-sample-a-data-frame/32791/4 "2019-12-30T10:30:44Z")

</div>

I’m not sure I understand - do you want to sample 100 rows from a 1,000 row `DataFrame`? Or do you want to draw 1,000 samples of length 100 from a larger data set? My suggestion above can work in both cases, can you clarify what you’re looking for (and what isn’t working for you) ideally by way of a [minimal working example](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757)?

---

<div class="post-metadata">

### Author: ![imrankhan\_juliaLang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/imrankhan_julialang/32/12020_2.png) [@imrankhan\_juliaLang](https://discourse.julialang.org/u/imrankhan_juliaLang)
#### Post date: [December 30, 2019, 10:35am UTC](https://discourse.julialang.org/t/how-to-sample-a-data-frame/32791/5 "2019-12-30T10:35:40Z")

</div>

yes i want 1,000 samples from length 100 data set

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [December 30, 2019, 10:45am UTC](https://discourse.julialang.org/t/how-to-sample-a-data-frame/32791/6 "2019-12-30T10:45:10Z")

</div>

Okay to adapt my example from above, you have a length 100 data set:

```julia
df = DataFrame(a = 1:100, b = rand(100))

```

now we can get 1,000 random samples from this - I’m assuming each sample has length 10 here:

```julia
samples = [df[shuffle(1:nrow(df))[1:10], :] for _ in 1:1_000]

```

`samples` is now a vector of lenght 1,000 which holds a 10-row random sample of your original data set in each location.

---

<div class="post-metadata">

### Author: ![robsmith11](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robsmith11/32/29641_2.png) [@robsmith11](https://discourse.julialang.org/u/robsmith11)
#### Post date: [December 30, 2019, 1:28pm UTC](https://discourse.julialang.org/t/how-to-sample-a-data-frame/32791/7 "2019-12-30T13:28:20Z")

</div>

Or if you’d like to sample 1,000 rows with replacement:

```julia
df[rand(1:nrow(df),1000),:]

```

---

<div class="post-metadata">

### Author: ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)
#### Post date: [December 30, 2019, 4:26pm UTC](https://discourse.julialang.org/t/how-to-sample-a-data-frame/32791/8 "2019-12-30T16:26:37Z")

</div>

I imagine you are trying to bootstrap data. In addition to the solutions given here, see if `bootstrap.jl` is a package that works for you.

---

<div class="post-metadata">

### Author: ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)
#### Post date: [December 31, 2019, 1:09am UTC](https://discourse.julialang.org/t/how-to-sample-a-data-frame/32791/9 "2019-12-31T01:09:42Z")

</div>

[DependentBootstrap](https://github.com/colintbowers/DependentBootstrap.jl) will also work here. One of the options is an iid bootstrap which will do what the OP wants, ie:

```julia
using DependentBootstrap
dbootdata(mydataframe, numresample=1000, bootmethod=:iid)

```

will return a vector of length `1000` where each element is a resampled `DataFrame`.

---

<div class="post-metadata">

### Author: ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)
#### Post date: [December 31, 2019, 2:03am UTC](https://discourse.julialang.org/t/how-to-sample-a-data-frame/32791/10 "2019-12-31T02:03:03Z")

</div>

This is how I split my DataFrame into “training” and “testing”

```julia
function createTrainTest(df::DataFrame,prop=0.5,randomseed=1234)
    df_training = similar(df,0)
    df_testing = similar(df,0)

    # Now split the df into df_training and df_testing
    df_size = size(df,1)
    training_proportion = prop
    trainingsize = round(df_size*training_proportion)

    # Create a random permutation vector
    randvec = randperm!(MersenneTwister(randomseed),
                        Vector{Int64}(undef,df_size))

    for k in axes(df)[1]
        push!( k ≤ trainingsize ?
                df_training : df_testing ,
                df[randvec[k],:]
        )
    end
    return (df_training,df_testing)
end

```

If you want 1000 samples with each sample having 100 rows then just change the trainingsize to a fixed value of 100 and call the above function 1000 times.

PS: do not forget to use a different randomseed each time!

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [December 31, 2019, 5:08am UTC](https://discourse.julialang.org/t/how-to-sample-a-data-frame/32791/11 "2019-12-31T05:08:57Z")

</div>

```julia
using StatsBase:sample
using DataFrames

df = DataFrame(a = 1:1000)

sample_rows = sample(1:nrow(df), 100, replace=false)

df_sample = df[sample_rows, :]

test_rows = setdiff(1:nrow(df), sample_rows)

df_test = df[test_rows, :]

```
