# Selecting rows

**URL:** <https://discourse.julialang.org/t/selecting-rows/82369>\
**Category:** New to Julia\
**Created:** [June 7, 2022, 12:36pm UTC](https://discourse.julialang.org/t/selecting-rows/82369 "2022-06-07T12:36:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gjh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gjh/32/31762_2.png) [@gjh](https://discourse.julialang.org/u/gjh)\
**Post date:** [June 7, 2022, 12:36pm UTC](https://discourse.julialang.org/t/selecting-rows/82369/1 "2022-06-07T12:36:33Z")

</div>

With this df what is an efficient way to get a new df with two copies of the x==2 row and one copy of x==3 using a needed\_rows vector?

x=[1,2,3]  
y=[“a”,“b”,“c”]  
df=DataFrame(x=x, y=y)

needed\_rows=[2,2,3]

The new df would be:

3×2 DataFrame  
Row │ x y  
│ Int64 String  
─────┼───────────────  
1 │ 2 b  
2 │ 2 b  
3 │ 3 c

Thank you for the help!

---

<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:** [June 7, 2022, 12:44pm UTC](https://discourse.julialang.org/t/selecting-rows/82369/2 "2022-06-07T12:44:26Z")

</div>

```julia
julia> df[needed_rows, :]
3×2 DataFrame
 Row │ x y
     │ Int64 String
─────┼───────────────
   1 │ 2 b
   2 │ 2 b
   3 │ 3 c

```

Please make sure to enclose your code in triple backticks ````` to make copy/pasting easier.
