Create new column in dataframe based on list

Hello, I’m trying to figure out how to create a 0,1 valued column in a dataframe based on comparing a dataframe column with a list. Here is an example

data = DataFrame(:index => String[], :values => Int64[])

push!(data, ["a",  1])
push!(data, ["b",  2])
push!(data, ["c",  1])

list = ["a","b"]

I’m trying to create a column called PASS which is 0,1 based on if the index column in this df contains one of the values in this list.

I’ve tried this, but this doesn’t seem to work

data.pass .= data[in.(data.index , list), :]

Any help with this?

Is this what you are looking for?

julia> data.pass = map(in(list), data.index)
3-element Vector{Bool}:
 1
 1
 0
2 Likes

if list is small use

transform!(data, :index => ByRow(in(list)) => :pass)

if it is large use

transform!(data, :index => ByRow(in(Set(list))) => :pass)
2 Likes

Thanks, this works! I guess it would be nice if there were a simple broadcast of the IN statement like in.(list) but I can use this. thanks.

1 Like
julia> in(list).(data[!,:index])
3-element BitVector:
 1
 1
 0

julia> in(Set(list)).(data[!,:index])
3-element BitVector:
 1
 1
 0
1 Like

the new column can be built like this

data.pass = in.(data.index , [list])

#or

data.repass= data.index .∈ [list]