# How to select rows of a dataframe?

**URL:** <https://discourse.julialang.org/t/how-to-select-rows-of-a-dataframe/72682>\
**Category:** General Usage\
**Tags:** dataframes\
**Created:** [December 6, 2021, 9:41pm UTC](https://discourse.julialang.org/t/how-to-select-rows-of-a-dataframe/72682 "2021-12-06T21:41:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![corny85](https://avatars.discourse-cdn.com/v4/letter/c/a88e4f/32.png) [@corny85](https://discourse.julialang.org/u/corny85)\
**Post date:** [December 6, 2021, 9:41pm UTC](https://discourse.julialang.org/t/how-to-select-rows-of-a-dataframe/72682/1 "2021-12-06T21:41:52Z")

</div>

Hello,

I have a dataframe `df` with 5 rows and a vector of vectors `v=[[3],[5],[1,2],[4]]`.  
I want to create a new dataframe `dg` with rows indexes sorted in order of `v`:

- the 1st row of `dg` is the 3rd row of `df`
- the 2nd row of `dg` is the 5th row of `df`
- the 3rd row of `dg` is the 1st row of `df`
- the 4th row of `dg` is the 2nd row of `df`
- the 5th row of `dg` is the 4th row of `df`

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![junder873](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@junder873](https://discourse.julialang.org/u/junder873)\
**Post date:** [December 6, 2021, 9:56pm UTC](https://discourse.julialang.org/t/how-to-select-rows-of-a-dataframe/72682/2 "2021-12-06T21:56:43Z")

</div>

Easiest way is probably to use `vcat` and splatting:

```julia
df = DataFrame(x=1:5)
v = [[3],[5],[1,2],[4]]
df[vcat(v...), :]

 Row │ x
     │ Int64
─────┼───────
   1 │ 3
   2 │ 5
   3 │ 1
   4 │ 2
   5 │ 4

```

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [December 6, 2021, 10:05pm UTC](https://discourse.julialang.org/t/how-to-select-rows-of-a-dataframe/72682/3 "2021-12-06T22:05:48Z")

</div>

Are you sure these indices in the vector couldn’t be replaced by the usual split-apply-combine workflow with dataframes? Maybe these indices are being computed from other columns of the original dataframe?

---

<div class="post-metadata">

**Author:** ![corny85](https://avatars.discourse-cdn.com/v4/letter/c/a88e4f/32.png) [@corny85](https://discourse.julialang.org/u/corny85)\
**Post date:** [December 6, 2021, 10:06pm UTC](https://discourse.julialang.org/t/how-to-select-rows-of-a-dataframe/72682/4 "2021-12-06T22:06:11Z")

</div>

It works !  
Thanks
