using DataFrames
df = DataFrame(A = 1:10, B = 2:2:20)
10x2 DataFrame
| Row | A | B |
|-----|----|----|
| 1 | 1 | 2 |
| 2 | 2 | 4 |
| 3 | 3 | 6 |
| 4 | 4 | 8 |
| 5 | 5 | 10 |
...
...
is it possible to subset dataframe by using a list of values, something like
df[df[:A] .in [3,4], :]
if the list is small, this can by done by
df[(df[:A] .== 3) | (df[:A] .== 4), :]
but I am wondering if there is way to do this for large list of values
The accepted answer above does not work anymore. So here is something working in 2019:
# by column name
julia> df[ [x in [3,4] for x in df[:A]] ,:]
2×2 DataFrame
│ Row │ A │ B │
│ │ Int64 │ Int64 │
├─────┼───────┼───────┤
│ 1 │ 3 │ 6 │
│ 2 │ 4 │ 8 │
# or by column number
julia> df[ [x in [3,4] for x in df[:1]] ,:]
2×2 DataFrame
│ Row │ A │ B │
│ │ Int64 │ Int64 │
├─────┼───────┼───────┤
│ 1 │ 3 │ 6 │
│ 2 │ 4 │ 8 │
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With