# In function runs really slow

**URL:** <https://discourse.julialang.org/t/in-function-runs-really-slow/78661>\
**Category:** Data\
**Tags:** dataframes\
**Created:** [March 29, 2022, 9:20am UTC](https://discourse.julialang.org/t/in-function-runs-really-slow/78661 "2022-03-29T09:20:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frankiewaang](https://avatars.discourse-cdn.com/v4/letter/f/f9ae1b/32.png) [@Frankiewaang](https://discourse.julialang.org/u/Frankiewaang)\
**Post date:** [March 29, 2022, 9:20am UTC](https://discourse.julialang.org/t/in-function-runs-really-slow/78661/1 "2022-03-29T09:20:34Z")

</div>

I got 2 DataFrames `tran` and `articles` and I want to filter those rows in `tran` that `tran.article_id` in `articles.article_id`, the function I use is:

```nohighlight
trans = transactions[in.(transactions.article_id, Ref(articles.article_id)),:]

```

in python I will use

```python
trans = trans[trans["article_id"].isin(articles["article_id"])].reset_index(drop=True)

```

In python, it took 12.7s to finish but in Julia it would take several minutes, is there any way to speed up the process

PS: maybe I should not ask the question here as it is the buid-in function that make the slicing so slow.

---

<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 29, 2022, 9:23am UTC](https://discourse.julialang.org/t/in-function-runs-really-slow/78661/2 "2022-03-29T09:23:16Z")

</div>

Hard to say for certain without an MWE, but you should probably construct a set to look up the values you want to check against:

```julia
transactions[in(Set(articles.article_id)).(transactions.article_id), :]

```

---

<div class="post-metadata">

**Author:** ![Frankiewaang](https://avatars.discourse-cdn.com/v4/letter/f/f9ae1b/32.png) [@Frankiewaang](https://discourse.julialang.org/u/Frankiewaang)\
**Post date:** [March 29, 2022, 9:31am UTC](https://discourse.julialang.org/t/in-function-runs-really-slow/78661/3 "2022-03-29T09:31:08Z")

</div>

Thank! I am just trying and it runs pretty fast. The `articles.article_id` is unique so I did not use Set at first, but I couldn’t figure out why using set could make it so much faster

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [March 29, 2022, 9:35am UTC](https://discourse.julialang.org/t/in-function-runs-really-slow/78661/4 "2022-03-29T09:35:32Z")

</div>

There is a tradeoff. If you are doing one `in` then not converting vector to `Set` is faster. If you are doing many lookups then it is better to do the conversion.

Because it cannot be known upfront what your use case is you need to be explicit and create `Set` if you expect to perform many lookups.
