# Dataframe to vector

**URL:** <https://discourse.julialang.org/t/dataframe-to-vector/83248>\
**Category:** New to Julia\
**Tags:** dataframes, vector\
**Created:** [June 23, 2022, 2:42pm UTC](https://discourse.julialang.org/t/dataframe-to-vector/83248 "2022-06-23T14:42:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gjh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gjh/32/31762_2.png) [@gjh](https://discourse.julialang.org/u/gjh)\
**Post date:** [June 23, 2022, 2:42pm UTC](https://discourse.julialang.org/t/dataframe-to-vector/83248/1 "2022-06-23T14:42:56Z")

</div>

How can one transform the tmp dataframe to be a 2-element Vector in the same format as rand(2) produces? Thank you for the help! Sorry for the simple questions.

using DataFrames  
x = rand(3)  
y = rand(3)  
z = rand(3)  
df = DataFrame(x=x, y=y, z=z)

tmp = df[2, [:x, :z]]

rand(2)

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [June 23, 2022, 3:02pm UTC](https://discourse.julialang.org/t/dataframe-to-vector/83248/2 "2022-06-23T15:02:07Z")

</div>

`collect(df[2, [:x, :z]])`

edit: one caveat for `collect` versus `Vector`/`Matrix` is that the latter coerces its arguments to a uniform element type if possible, which may or may not be what you want.

```julia
julia> df.y = 1:3; df
3×3 DataFrame
 Row │ x y z
     │ Float64 Int64 Float64
─────┼────────────────────────────
   1 │ 0.0116169 1 0.72888
   2 │ 0.257525 2 0.842221
   3 │ 0.350478 3 0.528759

julia> collect(df[2, [:x, :y]])
2-element Vector{Real}:
 0.25752498464044615
 2

julia> Vector(df[2, [:x, :y]])
2-element Vector{Float64}:
 0.25752498464044615
 2.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:** [June 23, 2022, 3:02pm UTC](https://discourse.julialang.org/t/dataframe-to-vector/83248/3 "2022-06-23T15:02:11Z")

</div>

```julia
Array(tmp)

```

or

```julia
Vector(tmp)

```

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [June 23, 2022, 7:31pm UTC](https://discourse.julialang.org/t/dataframe-to-vector/83248/4 "2022-06-23T19:31:32Z")

</div>

Side note. `tmp` is a `DataFrameRow` and this is the reason the proposed solutions work.

---

<div class="post-metadata">

**Author:** ![gjh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gjh/32/31762_2.png) [@gjh](https://discourse.julialang.org/u/gjh)\
**Post date:** [June 23, 2022, 8:26pm UTC](https://discourse.julialang.org/t/dataframe-to-vector/83248/5 "2022-06-23T20:26:46Z")

</div>

Thank you all for the help!

Why does the below change error (i.e, changed y column and how the dataframe row is selected)?  
ERROR: MethodError: no method matching length(::DataFrame)

using DataFrames  
x = rand(3)  
y = [“a”, “b”, “c”]  
z = rand(3)  
df = DataFrame(x=x, y=y, z=z)  
tmp = df[df.y .== “b”, [:x, :z]]  
collect(tmp)

* * *

tmp = df[2, [:x, :z]]  
collect(tmp) does not error

Thank you!

---

<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:** [June 23, 2022, 8:28pm UTC](https://discourse.julialang.org/t/dataframe-to-vector/83248/6 "2022-06-23T20:28:17Z")

</div>

> [@gjh](#):
>
> tmp = df[df.y .== “b”, [:x, :z]]

Please quote your quote in triple back-ticks

````julia
```
like this 
```

````

The reason is that `df.y .== "b"` produces a vector, not an integer. Therefore

```julia
df[df.y .== “b”, [:x, :z]]

```

produces a `DataFrame`, not a `DataFrameRow`.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [June 23, 2022, 8:49pm UTC](https://discourse.julialang.org/t/dataframe-to-vector/83248/7 "2022-06-23T20:49:54Z")

</div>

And in `DataFrame` case it makes no sense to convert it to a `Vector` since it is a 2-dimensional object; you can convert it to a matrix by writing `Matrix(tmp)`.
