Questions about filter and broadcast

df = DataFrame(x=[3, 1, 2, 1], y=["b", "c", "a", "b"])
4×2 DataFrame
 Row │ x      y
     │ Int64  String
─────┼───────────────
   1 │     3  b
   2 │     1  c
   3 │     2  a
   4 │     1  b

julia> filter(row->row.x==2,df)
1×2 DataFrame
 Row │ x      y
     │ Int64  String
─────┼───────────────
   1 │     2  a

But now I want to filter row.x=2 or 3,so i think of broadcast.and tried

a=[2,3]
julia> filter(row->row.x==a,df)
0×2 DataFrame
julia> filter.(row->row.x==a,df)
ERROR: MethodError: no method matching filter(::var"#31#32", ::Int64)
Closest candidates are:

I don’t know how to do.

For DataFrames you can pass a Pair as the first argument with a column name and a function to apply rowwise to that column.

Your main issue though has nothing to do with DataFrames but with the simple fact that

julia> 2 == [2, 3]
false

You are looking for containment, i.e. in:

julia> filter(:x => in([2, 3]), df)
2×2 DataFrame
 Row │ x      y
     │ Int64  String
─────┼───────────────
   1 │     3  b
   2 │     2  a

P.S. to make what you tried originally work more directly you could have checked whether the value was equal to 2 or to 3:

julia> filter(:x => row -> row==2||row==3, df)
2×2 DataFrame
 Row │ x      y
     │ Int64  String
─────┼───────────────
   1 │     3  b
   2 │     2  a

But that’s a bit cumbersome especially for larger collections.

1 Like