Select colums by the value of rows

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?

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

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

``````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
``````

You could also select columns via a predicate like so:

``````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
``````

use

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

Perhaps safer to not assume integers:

`````` df[:, [all(3 .ā¤ x .ā¤ 5) for x in eachcol(df)]]
``````