# Index data from DelimitedFiles by name

**URL:** <https://discourse.julialang.org/t/index-data-from-delimitedfiles-by-name/57284>\
**Category:** New to Julia\
**Tags:** indexing, csv\
**Created:** [March 16, 2021, 9:28am UTC](https://discourse.julialang.org/t/index-data-from-delimitedfiles-by-name/57284 "2021-03-16T09:28:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [March 16, 2021, 9:28am UTC](https://discourse.julialang.org/t/index-data-from-delimitedfiles-by-name/57284/1 "2021-03-16T09:28:16Z")

</div>

I know there is CSV.jl, but I am wondering what the easiest way to access the data by their column names would be if I just use DelimitedFiles.jl.

My first approach would look like this, but maybe there is a more elegant solution.

```julia
using DelimitedFiles
data,header = readdlm(path,header=true)
coldata = data[:,header .== "column name"]

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 16, 2021, 4:12pm UTC](https://discourse.julialang.org/t/index-data-from-delimitedfiles-by-name/57284/2 "2021-03-16T16:12:44Z")

</div>

You can convert the header and columns to a dict with, for example:

The data:

```julia
shell> more test.dat
A B C
1 2 3
1 2 3
1 2 3

```

```julia
julia> data, header = readdlm("./test.dat",header=true)
([1.0 2.0 3.0; 1.0 2.0 3.0; 1.0 2.0 3.0], AbstractString["A" "B" "C"])

julia> d = Dict(header[i] => data[:,i] for i in 1:length(header))
Dict{SubString{String},Array{Float64,1}} with 3 entries:
  "B" => [2.0, 2.0, 2.0]
  "A" => [1.0, 1.0, 1.0]
  "C" => [3.0, 3.0, 3.0]

julia> d["A"]
3-element Array{Float64,1}:
 1.0
 1.0
 1.0

```

That copies the data. If that is not desirable, you can use dictionary only associating the header with the column indexes:

```julia
julia> hind = Dict(header[i] => i for i in 1:length(header))
Dict{SubString{String},Int64} with 3 entries:
  "B" => 2
  "A" => 1
  "C" => 3

julia> data[:,hind["A"]]
3-element Array{Float64,1}:
 1.0
 1.0

```

Or if you are just willing to do this once in a while:

```julia
julia> data[:, findfirst(==("A"),header)]
3-element Array{Float64,1}:
 1.0
 1.0
 1.0

```

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [March 16, 2021, 5:07pm UTC](https://discourse.julialang.org/t/index-data-from-delimitedfiles-by-name/57284/3 "2021-03-16T17:07:18Z")

</div>

The DIct option looks very nice.  
Maybe views will remove the need to copy the data

```julia
 Dict(header[i]=>view(data,:,i) for i in 1:length(header))

```

or a named tuple(although the code looks a bit messy)

```julia
(;((Symbol.(header[i]),view(data,:,i)) for i in 1:length(header))...)

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 16, 2021, 5:20pm UTC](https://discourse.julialang.org/t/index-data-from-delimitedfiles-by-name/57284/4 "2021-03-16T17:20:05Z")

</div>

> [@feanor12](#):
>
> Maybe views will remove the need to copy the data

indeed
