# Easiest quickest way to search DataFrame w/wildcards

**URL:** https://discourse.julialang.org/t/easiest-quickest-way-to-search-dataframe-w-wildcards/35575
**Category:** Data
**Created:** [March 5, 2020, 12:51pm UTC](https://discourse.julialang.org/t/easiest-quickest-way-to-search-dataframe-w-wildcards/35575 "2020-03-05T12:51:56Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Usor](https://avatars.discourse-cdn.com/v4/letter/u/8dc957/32.png) [@Usor](https://discourse.julialang.org/u/Usor)
#### Post date: [March 5, 2020, 12:51pm UTC](https://discourse.julialang.org/t/easiest-quickest-way-to-search-dataframe-w-wildcards/35575/1 "2020-03-05T12:51:56Z")

</div>

Is there an easy straightforward already existing way to search a `DataFrame` with a `String` wildcard in a specific column, and then print the found entries/rows?

For example, you want to search if a certain name is in the data, but don’t remember it exactly. You wish to search with an approximate wildcard, such as `*batman*` (where the stars in this denote the wildcards).

If I can avoid regex, by the way, that’s good.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [March 5, 2020, 12:58pm UTC](https://discourse.julialang.org/t/easiest-quickest-way-to-search-dataframe-w-wildcards/35575/2 "2020-03-05T12:58:45Z")

</div>

`[c for c in names(dd) if occursin("batman", string(c))]`? For any more complex patterns I don’t see how you get by without regex.

You also might have noticed that DataFrames has a convenience function that improves error messages for typos of column names - if the column you’re requesting is close to the name of an existing column it will suggest that similarly named column. So if it’s really just about not exactly remembering a name you can just try an approximate name and let the error message help you.

---

<div class="post-metadata">

### Author: ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)
#### Post date: [March 5, 2020, 1:17pm UTC](https://discourse.julialang.org/t/easiest-quickest-way-to-search-dataframe-w-wildcards/35575/3 "2020-03-05T13:17:09Z")

</div>

Something like this?

```julia
julia> df = DataFrame(:a => ["foo", "bar", "baz"])
3×1 DataFrame
│ Row │ a │
│ │ String │
├─────┼────────┤
│ 1 │ foo │
│ 2 │ bar │
│ 3 │ baz │

julia> df[findall(x -> occursin("ba", x), df[!, :a]), :]
2×1 DataFrame
│ Row │ a │
│ │ String │
├─────┼────────┤
│ 1 │ bar │
│ 2 │ baz │

```

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [March 5, 2020, 1:27pm UTC](https://discourse.julialang.org/t/easiest-quickest-way-to-search-dataframe-w-wildcards/35575/4 "2020-03-05T13:27:39Z")

</div>

Ah sorry I read the question as asking about the name of a column rather than the data within a column. That said, why don’t you broadcast `occursin` like

```julia
julia> df[occursin.("ba", df.a), :]
2×1 DataFrame
│ Row │ a │
│ │ String │
├─────┼────────┤
│ 1 │ bar │
│ 2 │ baz │

```

---

<div class="post-metadata">

### Author: ![Usor](https://avatars.discourse-cdn.com/v4/letter/u/8dc957/32.png) [@Usor](https://discourse.julialang.org/u/Usor)
#### Post date: [March 5, 2020, 1:51pm UTC](https://discourse.julialang.org/t/easiest-quickest-way-to-search-dataframe-w-wildcards/35575/5 "2020-03-05T13:51:09Z")

</div>

Thanks, both. Very useful.

The use of `occursin` came to mind, but now you’ve shown me how to use it in this particular context.

Standing now in this case on your shoulders, I’m wondering if I can make a general quick function with this information. If I have time, I hope to do so.

---

<div class="post-metadata">

### Author: ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)
#### Post date: [March 5, 2020, 2:10pm UTC](https://discourse.julialang.org/t/easiest-quickest-way-to-search-dataframe-w-wildcards/35575/6 "2020-03-05T14:10:52Z")

</div>

It can be generalized a little

```julia
mysearch(smt, somewhere) = false
mysearch(smt::AbstractString, somewhere::AbstractString) = occursin(smt, somewhere)

findsomethingsomewhere(smt, df) =
    df[reduce((x, y) -> x .| mysearch.(smt, df[!, y]), names(df);
        init = BitArray([false for _ in 1:nrow(df)])), :]

```

```julia
julia> df = DataFrame(:a => ["foo", "bar", "baz", "baa"], :b => 1:4, :c => ["a", "b", "c", "ar"])
4×3 DataFrame
│ Row │ a │ b │ c │
│ │ String │ Int64 │ String │
├─────┼────────┼───────┼────────┤
│ 1 │ foo │ 1 │ a │
│ 2 │ bar │ 2 │ b │
│ 3 │ baz │ 3 │ c │
│ 4 │ baa │ 4 │ ar │

julia> julia> findsomethingsomewhere("ar", df)
2×3 DataFrame
│ Row │ a │ b │ c │
│ │ String │ Int64 │ String │
├─────┼────────┼───────┼────────┤
│ 1 │ bar │ 2 │ b │
│ 2 │ baa │ 4 │ ar │

```

---

<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: [March 5, 2020, 3:00pm UTC](https://discourse.julialang.org/t/easiest-quickest-way-to-search-dataframe-w-wildcards/35575/7 "2020-03-05T15:00:51Z")

</div>

Here is a somewhat more readable function

```julia
function findsomethingsomewhere(smt, df)
    positives = fill(false, nrow(df))
    for col in eachcol(df)
        if eltype(col) <: AbstractString
            positives .= positives .| occursin.(smt, col)
        end 
    end
    df[positives, :]
end

```

---

<div class="post-metadata">

### Author: ![Usor](https://avatars.discourse-cdn.com/v4/letter/u/8dc957/32.png) [@Usor](https://discourse.julialang.org/u/Usor)
#### Post date: [March 5, 2020, 3:21pm UTC](https://discourse.julialang.org/t/easiest-quickest-way-to-search-dataframe-w-wildcards/35575/8 "2020-03-05T15:21:54Z")

</div>

Great. Thank you both. That will be useful.
