Occursin with a vector

Hi everyone,

a = [“abc”, “def”,“ghi”]

I have an array a and a column of my dataframe df[:Column1].

Is it possible with the function “occursin” (or another one) to know if the elements of a are into df[:Column1] without making a loop “for i in a” ? Is there a way to broadcast occursin ?

One way:

using DataFrames
df = DataFrame(Col1 = ["abc", "aaf","ghi", "bbc", "baf","bhi"])
a = ["abc", "ghi", "bhi"]
df.Col1 .∈ Ref(a)
2 Likes

Thanks !

How to print those entries of a that are contained in df ? What if instead of df we have to match with text in string ?

Probably filter then.

filter(y -> y in df.x, a)

(This won’t be particularly fast, if that’s a problem)

There’s one of the functions of findall family that returns the index and the value of the elements, and could be used. But I don’t remember which one.

for these needs the indexin() function comes in handy


julia> df[indexin(a,df.Col1),:Col1]
3-element Vector{String}:
 "abc"
 "ghi"
 "bhi"
1 Like

@rocco_sprmnt21

do you mean like this?

str=join(["abc", "aaf","ghi", "bbc", "baf","bhi"])
a = ["abc", "ghi", "bhi"]

findall.(a,[str])
1 Like