Subset a dataframe by column of another dataframe

Ah I see.

Setup:

julia> df = DataFrame(a = [1, 2, 3, 4]); df2 = DataFrame(a2 = [1, 2]);

Indexing solution:


julia> df[in.(df.a, Ref(df2.a2)), :]
2×1 DataFrame
 Row │ a     
     │ Int64 
─────┼───────
   1 │     1
   2 │     2

filter solution

julia> filter(:a => in(df2.a2), df)

DataFramesMeta solution

julia> @where(df, in.(:a, Ref(df2.a2)))
2×1 DataFrame
 Row │ a     
     │ Int64 
─────┼───────
   1 │     1
   2 │     2

Note: Performance is actually better if you convert df2.a2 to a Set first, but you don’t have to worry about this if things are fast enough.

Also I think this requires Julia 1.5 or above.

1 Like