# DataFrame : eltypes with variable length

**URL:** <https://discourse.julialang.org/t/dataframe-eltypes-with-variable-length/6016>\
**Category:** Data\
**Tags:** data\
**Created:** [September 21, 2017, 8:39am UTC](https://discourse.julialang.org/t/dataframe-eltypes-with-variable-length/6016 "2017-09-21T08:39:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fred](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fred/32/14175_2.png) [@Fred](https://discourse.julialang.org/u/Fred)\
**Post date:** [September 21, 2017, 8:39am UTC](https://discourse.julialang.org/t/dataframe-eltypes-with-variable-length/6016/1 "2017-09-21T08:39:29Z")

</div>

Hi,

The function  
`readtable(filename, [keyword options])`

as an optional keyword

`eltypes::Vector – Specify the types of all columns. Defaults to [].`

When the exact size of the table, it is possible to specify the types of the columns

```julia
 x = readtable("data/data.csv", separator = '\t' , eltypes = [String, Float64, Float64, Float64, Float64])
4×5 DataFrames.DataFrame
│ Row │ id │ s1 │ s2 │ s3 │ s4 │
├─────┼──────┼──────────┼──────────┼──────────┼──────────┤
│ 1 │ "g1" │ 0.134978 │ 0.231912 │ 0.479582 │ 0.134978 │
│ 2 │ "g2" │ 0.972158 │ 0.437821 │ NA │ 0.848548 │
│ 3 │ "g3" │ 0.152925 │ NA │ 0.848548 │ 0.152925 │
│ 4 │ "g4" │ 0.813864 │ 0.972158 │ 0.917429 │ 0.813864 │

```

But if the size of the table is not known and I only know that the first column is of type String how I can set eltypes ?

`eltypes = [String, Float64...]]`

Thanks !

---

<div class="post-metadata">

**Author:** ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)\
**Post date:** [September 21, 2017, 9:29am UTC](https://discourse.julialang.org/t/dataframe-eltypes-with-variable-length/6016/2 "2017-09-21T09:29:39Z")

</div>

With the CSV.jl package, you can just do

```julia
CSV.read("data/data.csv", delim='\t', types=Dict(1=>String))

```

this will return a DataFrame by default.

---

<div class="post-metadata">

**Author:** ![Fred](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fred/32/14175_2.png) [@Fred](https://discourse.julialang.org/u/Fred)\
**Post date:** [September 21, 2017, 10:02am UTC](https://discourse.julialang.org/t/dataframe-eltypes-with-variable-length/6016/3 "2017-09-21T10:02:00Z")

</div>

Thank you very much Quinnj !

The reason I stay with DataFrames and readtable is that I have a better speed with readtable even if I don’t specify the eltypes. But it is possible that I have done something wrong.

```julia
$ julia DataCSV.jl 

WARNING: Method definition ==(Base.Nullable{S}, Base.Nullable{T}) in module Base at nullable.jl:238 overwritten in module NullableArrays at /home/fred/.julia/v0.6/NullableArrays/src/operators.jl:99.
Reading...	data.csv
Reading...	data2.csv
elapsed time: 3.95377051 seconds

$ julia DataFrames.jl 
Reading...	data.csv
Reading...	data2.csv
elapsed time: 1.566749483 seconds

```

DataCSV

```julia
using CSV

##########################################
# read dataframe
function readTable(file, sep, h)
    println("Reading...\t", file)
    x = CSV.read(file ; delim = sep, types=Dict(1=>String), header = h, null="NA") # read data file
    return x
end

function main()
    sep = '\t' # table separator
    h = true # table header
  
    # process data
    f = ["data.csv", "data2.csv"]
    
    for file in f
        tab = readTable(file, sep, h)
    end
end

##########################################

tic()
main()
toc()

```

DataFrames

```julia
using DataFrames

# read dataframe
function readTable(file, sep, h)
    println("Reading...\t", file)
    x = readtable(file , separator = sep, header = h) # read data file
    return x
end

function main()
    sep = '\t' # table separator
    h = true # table header
  
    # process data
    f = ["data.csv", "data2.csv"]
    
    for file in f
        tab = readTable(file, sep, h)
    end
end

##########################################

tic()
main()
toc()

```

```julia
data.csv (tab separator)
id	s1	s2	s3	s4
g1	0.1349779443	0.2319120248	0.4795815343	0.1349779443
g2	0.9721584522	0.4378209082	0.8485481786
g3	0.1529253099	0.8485481786	0.1529253099
g4	0.8138636984	0.9721584522	0.9174289651	0.8138636984

data2.csv
id	s1	s2	s3	s4
g1	0.2235082715	0.726445808	0.3964289063	0.2169791684
g2	0.6151192371	0.7863019568	0.6236194363	
g3	0.9810212048	0.2967554158	0.5556356032
g4	0.0347811024	0.5602313542	0.1317892775	0.4228049423

```
