# Better Way to Filter Strings from DataFrame?

**URL:** <https://discourse.julialang.org/t/better-way-to-filter-strings-from-dataframe/88012>\
**Category:** Data\
**Tags:** dataframes\
**Created:** [September 29, 2022, 10:10pm UTC](https://discourse.julialang.org/t/better-way-to-filter-strings-from-dataframe/88012 "2022-09-29T22:10:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![r0b0ty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/r0b0ty/32/27250_2.png) [@r0b0ty](https://discourse.julialang.org/u/r0b0ty)\
**Post date:** [September 29, 2022, 10:10pm UTC](https://discourse.julialang.org/t/better-way-to-filter-strings-from-dataframe/88012/1 "2022-09-29T22:10:23Z")

</div>

I have a _DataFrame_ containing a column of **Strings** and a separate, standalone **Vector** of strings. I want a resulting _DataFrame_ with the rows containing the strings in the standalone vector… basically check the _text_ column against each string entry in the vector.

I did this, but seems overly convoluted for this task. **Is there a simpler or more efficient way?**

Thanks!

```julia
julia> df = DataFrame(:number => [1, 2, 3, 4], :text => ["Green Car", "Purple Grape", "Yellow Banana", "Purple Bruise"])
4×2 DataFrame
 Row │ number text
     │ Int64 String
─────┼───────────────────────
   1 │ 1 Green Car
   2 │ 2 Purple Grape
   3 │ 3 Yellow Banana
   4 │ 4 Purple Bruise

julia> keyword = ["Green", "Brown", "Black", "Blue"]
4-element Vector{String}:
 "Green"
 "Brown"
 "Black"
 "Blue"

julia> result = DataFrame(:number => Integer[], :text => String[])
0×2 DataFrame

julia> for color in keyword
           append!(result, df[contains.(df.text, color), :])
       end

julia> result
1×2 DataFrame
 Row │ number text
     │ Integer String
─────┼────────────────────
   1 │ 1 Green Car

```

---

<div class="post-metadata">

**Author:** ![AndiMD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andimd/32/6531_2.png) [@AndiMD](https://discourse.julialang.org/u/AndiMD)\
**Post date:** [September 29, 2022, 11:05pm UTC](https://discourse.julialang.org/t/better-way-to-filter-strings-from-dataframe/88012/2 "2022-09-29T23:05:10Z")

</div>

Hi!  
How about this:  
`filter(row->any(occursin.(keyword,row.text)),df )`

---

<div class="post-metadata">

**Author:** ![r0b0ty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/r0b0ty/32/27250_2.png) [@r0b0ty](https://discourse.julialang.org/u/r0b0ty)\
**Post date:** [September 29, 2022, 11:33pm UTC](https://discourse.julialang.org/t/better-way-to-filter-strings-from-dataframe/88012/3 "2022-09-29T23:33:42Z")

</div>

Amazingly simple, **thank you**! I had tried a variation of that, but without the `any()` function which is probably why it didn’t work.

FYI, I tried it with the `contains()` function too and it worked - I wouldn’t have thought to use `occursin()`.

```julia
filter(row -> any(occursin.(keyword, row.text)), df)
1×2 DataFrame
 Row │ number text      
     │ Int64 String    
─────┼───────────────────
   1 │ 1 Green Car

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [September 30, 2022, 1:28pm UTC](https://discourse.julialang.org/t/better-way-to-filter-strings-from-dataframe/88012/4 "2022-09-30T13:28:51Z")

</div>

With DataFramesMeta.jl one solution is

```julia
julia> @subset df occursin.(keyword, :text)
1×2 DataFrame
 Row │ number text      
     │ Int64 String    
─────┼───────────────────
   1 │ 1 Green Car

```
