# How to convert a dataframe into a 1-D vector, line by line?

**URL:** <https://discourse.julialang.org/t/how-to-convert-a-dataframe-into-a-1-d-vector-line-by-line/122594>\
**Category:** General Usage\
**Tags:** dataframes, vector\
**Created:** [November 13, 2024, 3:33pm UTC](https://discourse.julialang.org/t/how-to-convert-a-dataframe-into-a-1-d-vector-line-by-line/122594 "2024-11-13T15:33:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ju\_ska](https://avatars.discourse-cdn.com/v4/letter/j/ecd19e/32.png) [@Ju\_ska](https://discourse.julialang.org/u/Ju_ska)\
**Post date:** [November 13, 2024, 3:33pm UTC](https://discourse.julialang.org/t/how-to-convert-a-dataframe-into-a-1-d-vector-line-by-line/122594/1 "2024-11-13T15:33:27Z")

</div>

Hi,

I have a dataframe looking like:

```julia
using DataFrames

df = DataFrame(a=1:4, b=5:8)

```

```julia
4×2 DataFrame
 Row │ a b     
     │ Int64 Int64 
─────┼──────────────
   1 │ 1 5
   2 │ 2 6
   3 │ 3 7
   4 │ 4 8

```

From that, I would like to create a vector/array like:

```julia
[1, 5, 2, 6, 3, 7, 4, 8]

```

Tried many hcat, vcat, values etc. combinations without success…

---

<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:** [November 13, 2024, 3:40pm UTC](https://discourse.julialang.org/t/how-to-convert-a-dataframe-into-a-1-d-vector-line-by-line/122594/2 "2024-11-13T15:40:45Z")

</div>

```julia-repl
julia> v = Int[]
Int64[]

julia> for i in eachindex(df.a, df.b)
           push!(v, df.a[i])
           push!(v, df.b[i])
       end

julia> v
8-element Vector{Int64}:
 1
 5
 2
 6
 3
 7
 4
 8

```

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [November 13, 2024, 3:46pm UTC](https://discourse.julialang.org/t/how-to-convert-a-dataframe-into-a-1-d-vector-line-by-line/122594/3 "2024-11-13T15:46:29Z")

</div>

I’m not sure about performance, but here’s a one-liner:

```julia
df |> Matrix |> vec

```

and if you need the specific ordering of the elements:

```julia
vec(Matrix(df)')

# or df |> Matrix |> transpose |> vec

```

---

<div class="post-metadata">

**Author:** ![Ju\_ska](https://avatars.discourse-cdn.com/v4/letter/j/ecd19e/32.png) [@Ju\_ska](https://discourse.julialang.org/u/Ju_ska)\
**Post date:** [November 13, 2024, 4:25pm UTC](https://discourse.julialang.org/t/how-to-convert-a-dataframe-into-a-1-d-vector-line-by-line/122594/4 "2024-11-13T16:25:50Z")

</div>

Thanks everyone !

---

<div class="post-metadata">

**Author:** ![TimG](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@TimG](https://discourse.julialang.org/u/TimG)\
**Post date:** [November 13, 2024, 4:29pm UTC](https://discourse.julialang.org/t/how-to-convert-a-dataframe-into-a-1-d-vector-line-by-line/122594/5 "2024-11-13T16:29:22Z")

</div>

> [@Ju\_ska](#):
>
> ```julia
> using DataFrames
> 
> df = DataFrame(a=1:4, b=5:8)
> 
> ```

```julia
julia> vcat(df.a, df.b)
8-element Vector{Int64}:
 1
 2
 3
 4
 5
 6
 7
 8

```

Ahem! Didn’t read the Q carefully enough!

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [November 13, 2024, 4:54pm UTC](https://discourse.julialang.org/t/how-to-convert-a-dataframe-into-a-1-d-vector-line-by-line/122594/6 "2024-11-13T16:54:00Z")

</div>

other ways

```julia
vcat(eachcol(permutedims(df))...)

stack(permutedims(df),:).value

r,c=size(df)
vc=Vector{typeof(df[1,1])}(undef,r*c)
idx=0
for i in 1:r, j in 1:c
    vc[idx+=1]=df[i,j]
end
vc

```

```julia
Matrix(df)'[:]

```

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [November 14, 2024, 4:29pm UTC](https://discourse.julialang.org/t/how-to-convert-a-dataframe-into-a-1-d-vector-line-by-line/122594/7 "2024-11-14T16:29:19Z")

</div>

🤷‍♂️

```julia-repl
julia> df |> eachrow |> Iterators.flatten |> collect
8-element Vector{Int64}:
 1
 5
 2
 6
 3
 7
 4
 8

```
