# Best way to to handle DataFrames and write legible code

**URL:** https://discourse.julialang.org/t/best-way-to-to-handle-dataframes-and-write-legible-code/7368
**Category:** General Usage
**Created:** [November 28, 2017, 8:00pm UTC](https://discourse.julialang.org/t/best-way-to-to-handle-dataframes-and-write-legible-code/7368 "2017-11-28T20:00:15Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![IljaK91](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iljak91/32/44301_2.png) [@IljaK91](https://discourse.julialang.org/u/IljaK91)
#### Post date: [November 28, 2017, 8:00pm UTC](https://discourse.julialang.org/t/best-way-to-to-handle-dataframes-and-write-legible-code/7368/1 "2017-11-28T20:00:15Z")

</div>

What is the best way to get data out of DataFrames/save data in DataFrames and keep code legible? I quickly have the problem that my code looks something like this:

```julia
df_special_dataframe[(df_special_dataframe[:Date].>=Year).&(df_special_dataframe[:variable1].>=Threshold),:variable1] = An expression of similar length

```

That makes my code hard to read and work on. I see that part of the solution is to use the shortest names as possible for variables and DataFrames, but is there a better way to handle DataFrames in general?

I know from VBA that there is an “with…end” environment that looks like this:

```julia
With theCustomer.Comments
        .Add("First comment.")
        .Add("Second comment.")
End With

```

It basically saves you typing “theCustomer.Comments” again. I can imagine that a similar functionality could be implemented for DataFrames or is already existing.

---

<div class="post-metadata">

### Author: ![NickNack](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nicknack/32/1792_2.png) [@NickNack](https://discourse.julialang.org/u/NickNack)
#### Post date: [November 28, 2017, 10:55pm UTC](https://discourse.julialang.org/t/best-way-to-to-handle-dataframes-and-write-legible-code/7368/2 "2017-11-28T22:55:44Z")

</div>

I’m sure someone else has a better solution, but I would just make a short alias for the dataframe and put the filter in a helper variable. I think this is free (i.e. doesn’t cost extra allocations) and it is much more readable.

```julia
df = df_special_dataframe
rows = (df[:Date].>=Year) .& (df[:variable1].>=Threshold)
df[rows, :variable1] = expression 

```

Just a side note about my pet peeve, the precedence of `&`. Hopefully the parentheses will no longer be required in Julia 1.0. The [relevant issue has been reopened](https://github.com/JuliaLang/julia/issues/5187#issuecomment-328408905) and Jeff Bezanson is having a look at it. 🙂

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [November 28, 2017, 11:18pm UTC](https://discourse.julialang.org/t/best-way-to-to-handle-dataframes-and-write-legible-code/7368/3 "2017-11-28T23:18:40Z")

</div>

One suggestion is to use Query.jl. It’s legible once you get the hang of it.  
DataFramesMeta.jl has a `@with` macro that does what you describe.

---

<div class="post-metadata">

### Author: ![IljaK91](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iljak91/32/44301_2.png) [@IljaK91](https://discourse.julialang.org/u/IljaK91)
#### Post date: [November 29, 2017, 10:36am UTC](https://discourse.julialang.org/t/best-way-to-to-handle-dataframes-and-write-legible-code/7368/4 "2017-11-29T10:36:32Z")

</div>

Thanks, Query and DataFramesMeta look very interesting! I will have a look at them at the next opportunity 🙂
