Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

julia dataframe - subsetting column by list of values

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

like image 416
muon Avatar asked Sep 11 '25 16:09

muon


1 Answers

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     │
like image 50
Timothée HENRY Avatar answered Sep 13 '25 10:09

Timothée HENRY