# Can I have vectors in DataFrame cells?

**URL:** <https://discourse.julialang.org/t/can-i-have-vectors-in-dataframe-cells/58388>\
**Category:** New to Julia\
**Tags:** dataframes\
**Created:** [April 1, 2021, 7:53pm UTC](https://discourse.julialang.org/t/can-i-have-vectors-in-dataframe-cells/58388 "2021-04-01T19:53:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dkzwart](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@dkzwart](https://discourse.julialang.org/u/dkzwart)\
**Post date:** [April 1, 2021, 7:53pm UTC](https://discourse.julialang.org/t/can-i-have-vectors-in-dataframe-cells/58388/1 "2021-04-01T19:53:19Z")

</div>

I would to transfer information from a Matlab table to Julia. Julia does not directly import MAT tables, so I exported to CSV. The final column of my Matlab table is a column of vectors of 100000 data points each. Exporting to CSV, this last column becomes one column for each value in the vector. So when I import it into Julia, it still has a column for each data point in the vector. Can I have a column of vectors in a DataFrame? If so, how do I initialize it, and what is the best way to bring the data from 100000 columns into one column?  
If a DataFrame can not have vectors, is there a better way to bring the 100000 columns into vectors for each row than nested for loops?  
Thanks!

 ![image](https://global.discourse-cdn.com/julialang/original/3X/b/1/b17974831635f202adf48983238956705f12cb11.png)

---

<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:** [April 1, 2021, 7:57pm UTC](https://discourse.julialang.org/t/can-i-have-vectors-in-dataframe-cells/58388/2 "2021-04-01T19:57:13Z")

</div>

Have you checked out [MAT.jl](https://github.com/JuliaIO/MAT.jl)?

---

<div class="post-metadata">

**Author:** ![dkzwart](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@dkzwart](https://discourse.julialang.org/u/dkzwart)\
**Post date:** [April 1, 2021, 8:09pm UTC](https://discourse.julialang.org/t/can-i-have-vectors-in-dataframe-cells/58388/3 "2021-04-01T20:09:12Z")

</div>

Yes. It does not yet support Matlab tables

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [April 1, 2021, 8:21pm UTC](https://discourse.julialang.org/t/can-i-have-vectors-in-dataframe-cells/58388/4 "2021-04-01T20:21:58Z")

</div>

> [@dkzwart](#):
>
> Can I have a column of vectors in a DataFrame?

Yes! This works just like you might hope:

```julia
julia> using DataFrames

julia> df = DataFrame(A = [1, 2, 3], B = [[1, 2, 3], [4, 5, 6], [7, 8, 9]])
3×2 DataFrame
 Row │ A B         
     │ Int64 Array…    
─────┼──────────────────
   1 │ 1 [1, 2, 3]
   2 │ 2 [4, 5, 6]
   3 │ 3 [7, 8, 9]

```

You can also create an empty `DataFrame` with a column whose elements are themselves vectors, and you can push new rows to that data frame:

```julia
julia> df = DataFrame(A = Vector{Int}(), B = Vector{Vector{Int}}())
0×2 DataFrame

julia> push!(df, (1, [1, 2, 3]))
1×2 DataFrame
 Row │ A B         
     │ Int64 Array…    
─────┼──────────────────
   1 │ 1 [1, 2, 3]

```

As for handling your CSV import, I don’t know of a clever way (hopefully someone else here does), but bear in mind that loops in Julia are fast, so if you can solve your problem with a loop that’s often the fastest way to do it anyway.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 3, 2021, 10:54am UTC](https://discourse.julialang.org/t/can-i-have-vectors-in-dataframe-cells/58388/5 "2021-04-03T10:54:11Z")

</div>

Just in case it might interest, the code below takes the following CSV input example with a data vector:

```julia
Name,Year,DataVector
Baseline,1999,-3.1,0,1.5
Monitor1,2000,-1,-2,-3
Monitor2,2001,0,1.2,2

```

```julia
using DelimitedFiles, DataFrames

f = readdlm("CSV_arrays.csv", ',')
N = 2; # number of columns before data vector
Nr, Nc = size(f)
df1 = DataFrame(view(f,2:Nr,1:N), Symbol.(f[1,1:N]))
df2 = DataFrame(DataVector = [Float64.(view(f,i,N+1:Nc)) for i in 2:Nr])
df = hcat(df1, df2, makeunique=true)
df[!, :Name] = convert.(String, df[:, :Name])
df[!, :Year] = convert.(Int, df[:, :Year])

```

to produce:

```julia
julia> df
3×3 DataFrame
 Row │ Name Year DataVector
     │ String Int64 Array…
─────┼─────────────────────────────────────
   1 │ Baseline 1999 [-3.1, 0.0, 1.5]
   2 │ Monitor1 2000 [-1.0, -2.0, -3.0]
   3 │ Monitor2 2001 [0.0, 1.2, 2.0]

```
