# Select colums by the value of rows

**URL:** https://discourse.julialang.org/t/select-colums-by-the-value-of-rows/84793
**Category:** General Usage
**Tags:** question, dataframes
**Created:** [July 26, 2022, 6:23am UTC](https://discourse.julialang.org/t/select-colums-by-the-value-of-rows/84793 "2022-07-26T06:23:02Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![chenr86](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chenr86/32/36785_2.png) [@chenr86](https://discourse.julialang.org/u/chenr86)
#### Post date: [July 26, 2022, 6:23am UTC](https://discourse.julialang.org/t/select-colums-by-the-value-of-rows/84793/1 "2022-07-26T06:23:03Z")

</div>

Hello,

I want to select target colums with row values between an interval. For example, with df like below, I want to select colums with row values between [3, 5], which means that it should return `:x1 :x2 :y1`. How can I do that?

```julia
df = DataFrame(a=1, b=2, x1=3, x2=4, y1=5, y2=6)

```

---

<div class="post-metadata">

### Author: ![chenr86](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chenr86/32/36785_2.png) [@chenr86](https://discourse.julialang.org/u/chenr86)
#### Post date: [July 26, 2022, 7:49am UTC](https://discourse.julialang.org/t/select-colums-by-the-value-of-rows/84793/2 "2022-07-26T07:49:07Z")

</div>

For someone interested on this topic, the solution can be:

```julia
df_filter = DataFrame()
for i in 1:size(df, 2)
    if 3 <= df[end, i] <= 5
        df_filter[!, "Plan$i"] = df[:, i]
    end
end
df_filter

```

---

<div class="post-metadata">

### Author: ![Jollywatt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jollywatt/32/202198_2.png) [@Jollywatt](https://discourse.julialang.org/u/Jollywatt)
#### Post date: [July 26, 2022, 8:08am UTC](https://discourse.julialang.org/t/select-colums-by-the-value-of-rows/84793/3 "2022-07-26T08:08:47Z")

</div>

You could also select columns via a predicate like so:

```julia
julia> collect(df[1, :])
6-element Vector{Int64}:
 1
 2
 3
 4
 5
 6

julia> 3 .<= ans .<= 5
6-element BitVector:
 0
 0
 1
 1
 1
 0

julia> df[!, ans] # use ! to return a view; use : to copy
1×3 DataFrame
 Row │ x1 x2 y1
     │ Int64 Int64 Int64
─────┼─────────────────────
   1 │ 3 4 5

```

---

<div class="post-metadata">

### Author: ![DataFrames](https://avatars.discourse-cdn.com/v4/letter/d/e19b73/32.png) [@DataFrames](https://discourse.julialang.org/u/DataFrames)
#### Post date: [August 5, 2022, 4:29am UTC](https://discourse.julialang.org/t/select-colums-by-the-value-of-rows/84793/4 "2022-08-05T04:29:09Z")

</div>

use

```julia
df[:, [all(in(3:5), x) for x in eachcol(df)]]

```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [August 5, 2022, 2:32pm UTC](https://discourse.julialang.org/t/select-colums-by-the-value-of-rows/84793/5 "2022-08-05T14:32:55Z")

</div>

Perhaps safer to not assume integers:

```julia
 df[:, [all(3 .≤ x .≤ 5) for x in eachcol(df)]]

```
