How to get the values of a column according to multiple conditions of another column?

I would like to get values from a column according to multiple conditions of another column.
Here is an example of what i would like to do:

list_of_values = df[df[:column1] .== (value1 OR value2),:].column2

I know that for a condition it works like this:

list_of_values = df[df[:column1] .== value1,:].column2

I hope you can help me.


list_of_values = df[in.(df[:column1], Ref([value1, value2]),:].column2

Note, if df[:column] works for you, you are probably using a very old version of DataFrames. This could cause problems in other areas.

1 Like

Are you required to get the subset by using brackets? This use case seems like good reason to use filter. You could do something like

filter(:column1 => (c1 -> c1 == value1 || c1 == value2), df).column2

df[df[:,:col1] .∈ Ref([val1,val2]),:].col2 , seems a bit closer match to the syntax in your question.

For example:

using DataFrames
df = DataFrame((x=rand(1:10,10),y=rand("hello",10)))
df[df[:,:x] .∈ Ref([7,9]),:].y

I’d write

df[in([value1, value2]).(df.column1), :column2]
1 Like