# Filter rows using a filter table

**URL:** <https://discourse.julialang.org/t/filter-rows-using-a-filter-table/51975>\
**Category:** Data\
**Created:** [December 17, 2020, 12:05pm UTC](https://discourse.julialang.org/t/filter-rows-using-a-filter-table/51975 "2020-12-17T12:05:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [December 17, 2020, 12:05pm UTC](https://discourse.julialang.org/t/filter-rows-using-a-filter-table/51975/1 "2020-12-17T12:05:02Z")

</div>

I have a table whose rows I want to filter (by choosing or excluding them) on the basis of a series of criteria contained in a table. The criteria table has column fields corresponding by position to those of the data table. Each row of the table must be compared with all the criteria and if at least one is satisfied it is selected, otherwise it is discarded. The comparison is a simple pattern matching of the data row content with the criteria row pattern. To meet a criterion, the data row values must match one by one with the patterns in the criteria row. Although I haven’t done many tests to verify, I believe I have found a solution. But I would like to see other more succinct and / or more performing and / or more idiomatic or simply different solutions.  
I would also like if someone also made observations on parts of the script that can be done differently to improve something (anything).

```julia
criteria=DataFrame(Col1=["a1", "a2", ".*", ".*", "a4"],
                   Col2=[".*", "hfg", ".*end\$", ".*", "^start.*"],
                   Col3=[".*", ".*", "all3", ".*btw.*", ".*"])

df=DataFrame(col1=["a1", "a2", "a1", "a2", "a3", "a4", "a2", "a5"],
             col2=["bbbEnd","hfg", "start1","3eND","start3","cccEND","start5","alend"],
             col3=["tutto", "all3","btw3", "abtw4", "all2", "all3", "all4", "all2"])  

```

```
function matchdot(ptrn, str)
    mtch=true
    for i in 1:length(str)
        mtch=mtch && (occursin(str[1],ptrn[i])||occursin(Regex(ptrn[i],"i"),str[i]))
    end
    return mtch
end

function matchSomeCriteria(crt, row)
    match=false
    for r in eachrow(crt)
        match=match||matchdot(r,row)
    end
    return match
end
filter(v -> !matchSomeCriteria(criteria,v),df)
filter(v -> matchSomeCriteria(criteria,v),df)

```

---

<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:** [December 17, 2020, 6:04pm UTC](https://discourse.julialang.org/t/filter-rows-using-a-filter-table/51975/2 "2020-12-17T18:04:09Z")

</div>

Seems like a reasonable solution!
