# Can I reference a range within a dataframe using named columns?

**URL:** https://discourse.julialang.org/t/can-i-reference-a-range-within-a-dataframe-using-named-columns/63943
**Category:** New to Julia
**Tags:** dataframes
**Created:** [July 2, 2021, 2:41pm UTC](https://discourse.julialang.org/t/can-i-reference-a-range-within-a-dataframe-using-named-columns/63943 "2021-07-02T14:41:44Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Mitch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mitch/32/24224_2.png) [@Mitch](https://discourse.julialang.org/u/Mitch)
#### Post date: [July 2, 2021, 2:41pm UTC](https://discourse.julialang.org/t/can-i-reference-a-range-within-a-dataframe-using-named-columns/63943/1 "2021-07-02T14:41:44Z")

</div>

Hi,

I have a DataFrame (df) whose columns are titled col1, col2, 0, 1, 2, 3, 4 and 5.  
I need to create a vector referencing the first row (1) of columns 0, 1, 2, 3, 4 and 5.

The following code works, but isn’t dynamic.

```julia
vector = Array(df[1, 3:8])

```

I changed the titles to yr0, yr2 etc., and changed the code to the below, but get "MethodError: no method matching -(::String, ::String)

```julia
vector = Array(df[1, "yr0":"yr1"])

```

Is there a way to pull in a vector/array that’s dynamic and based on the title of the column vs. index reference? I’m new to Julia and coding, but thank you for your help!

---

<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: [July 2, 2021, 2:54pm UTC](https://discourse.julialang.org/t/can-i-reference-a-range-within-a-dataframe-using-named-columns/63943/2 "2021-07-02T14:54:25Z")

</div>

Check out `? Between`.

```julia
julia> using DataFrames

julia> df = DataFrame(a = 1, b1 = 2, b2 = 3, b4 = 5);

julia> df[:, Cols(:a, Between(:b2, :b4))]
1×3 DataFrame
 Row │ a b2 b4    
     │ Int64 Int64 Int64 
─────┼─────────────────────
   1 │ 1 3 5

```

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [July 2, 2021, 2:55pm UTC](https://discourse.julialang.org/t/can-i-reference-a-range-within-a-dataframe-using-named-columns/63943/3 "2021-07-02T14:55:53Z")

</div>

Here are some ways:

```julia
julia> df = DataFrame(rand(1:5, 3, 8), ["col1", "col2", string.(0:5)...])
3×8 DataFrame
 Row │ col1 col2 0 1 2 3 4 5     
     │ Int64 Int64 Int64 Int64 Int64 Int64 Int64 Int64 
─────┼────────────────────────────────────────────────────────
   1 │ 1 2 3 4 1 5 3 2
   2 │ 5 1 2 5 3 4 1 4
   3 │ 3 4 1 4 2 4 1 1

# Brute force
df[1, ["0", "1", "2", "3", "4", "5"]]

# Same but generating the strings dynamically
df[1, string.(0:5)]

# Using a regular expression matching "0", ..., "5"
df[1, r"^[0-5]$"]

# Specifying the first and last column to extract
df[1, Between("0", "5")]

```

---

<div class="post-metadata">

### Author: ![Mitch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mitch/32/24224_2.png) [@Mitch](https://discourse.julialang.org/u/Mitch)
#### Post date: [July 2, 2021, 3:01pm UTC](https://discourse.julialang.org/t/can-i-reference-a-range-within-a-dataframe-using-named-columns/63943/4 "2021-07-02T15:01:05Z")

</div>

Thank you very much!

---

<div class="post-metadata">

### Author: ![Mitch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mitch/32/24224_2.png) [@Mitch](https://discourse.julialang.org/u/Mitch)
#### Post date: [July 2, 2021, 3:01pm UTC](https://discourse.julialang.org/t/can-i-reference-a-range-within-a-dataframe-using-named-columns/63943/5 "2021-07-02T15:01:34Z")

</div>

Cheers, that’s super helpful!
