# How would I set a generalized condition that can be passed in to index a dataframe without explicitly defining the dataframe name

**URL:** <https://discourse.julialang.org/t/how-would-i-set-a-generalized-condition-that-can-be-passed-in-to-index-a-dataframe-without-explicitly-defining-the-dataframe-name/35293>\
**Category:** General Usage\
**Created:** [February 28, 2020, 5:53pm UTC](https://discourse.julialang.org/t/how-would-i-set-a-generalized-condition-that-can-be-passed-in-to-index-a-dataframe-without-explicitly-defining-the-dataframe-name/35293 "2020-02-28T17:53:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Julia1](https://avatars.discourse-cdn.com/v4/letter/j/db5fbb/32.png) [@Julia1](https://discourse.julialang.org/u/Julia1)\
**Post date:** [February 28, 2020, 5:53pm UTC](https://discourse.julialang.org/t/how-would-i-set-a-generalized-condition-that-can-be-passed-in-to-index-a-dataframe-without-explicitly-defining-the-dataframe-name/35293/1 "2020-02-28T17:53:46Z")

</div>

Here is what I have now:

df = df[df[Symbol(“PURCHASE”)].==“SUCCESS”, :]

what I’d like to do is set a success condition variable and have something that works like the code below:

success\_condition = Symbol(“PURCHASE”)].==“SUCCESS”

df = df[df[success\_condition]]

The key is that it should be able to be passed into any dataframe, not just one named df. I need it generalized if possible.

Thank you for your time and help!

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [February 28, 2020, 6:07pm UTC](https://discourse.julialang.org/t/how-would-i-set-a-generalized-condition-that-can-be-passed-in-to-index-a-dataframe-without-explicitly-defining-the-dataframe-name/35293/2 "2020-02-28T18:07:23Z")

</div>

First, please remember to quote your code with backticks.

Strictly speaking I’m not sure you can do what you want. The `Symbol("PURCHASE")` only refers to a column of a DataFrame when it is actually passed in to one as `df[!,Symbol("PURCHASE")]`. Outside of that it will just look like `:PURCHASE`, which is just a variable of type `Symbol`.

What you could do is to define a function like so

```julia
findsuccess(v) = findall( x -> x == "SUCCESS",v)

```

and then pass to it the DataFrame column `findsuccess(df[!,Symbol("PURCHASE")])`

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [February 28, 2020, 7:55pm UTC](https://discourse.julialang.org/t/how-would-i-set-a-generalized-condition-that-can-be-passed-in-to-index-a-dataframe-without-explicitly-defining-the-dataframe-name/35293/3 "2020-02-28T19:55:27Z")

</div>

An easy way is to use Query.jl:

```julia
func = x -> x.PURCHASE == "SUCCESS"
df |> @filter(func(_))

```

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [February 29, 2020, 4:15pm UTC](https://discourse.julialang.org/t/how-would-i-set-a-generalized-condition-that-can-be-passed-in-to-index-a-dataframe-without-explicitly-defining-the-dataframe-name/35293/4 "2020-02-29T16:15:19Z")

</div>

Or just `filter(x -> x.PURCHASE == "SUCCESS", df)`. You can store `x -> x.PURCHASE` in a variable to apply it to any data frame.
