# Create data frame using values from vector as columns

**URL:** <https://discourse.julialang.org/t/create-data-frame-using-values-from-vector-as-columns/105416>\
**Category:** General Usage\
**Tags:** dataframes, reshaping, matrix\
**Created:** [October 26, 2023, 4:44am UTC](https://discourse.julialang.org/t/create-data-frame-using-values-from-vector-as-columns/105416 "2023-10-26T04:44:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sandy45](https://avatars.discourse-cdn.com/v4/letter/s/3ec8ea/32.png) [@Sandy45](https://discourse.julialang.org/u/Sandy45)\
**Post date:** [October 26, 2023, 4:44am UTC](https://discourse.julialang.org/t/create-data-frame-using-values-from-vector-as-columns/105416/1 "2023-10-26T04:44:44Z")

</div>

I have data frame which has column say “minutes” and I need to create another data frame using “minutes” as columns as shown below.

I was able to create using reshape but values are being concatenated where I need them in vertical.

```julia
julia> df.values
89011-element Vector{Float64}:
 122.50652071263562
 122.72935583046971
 122.95219094830381
 123.180309439943
 123.40987110571591
 123.63943277148881
   ⋮
 249.6565598272147
 249.9075845099697
 250.16789295401594
 250.43361068950563
 250.69932842499531
 250.965046160485

```

```julia
julia> df = DataFrame(reshape(df.minutes, (nrow(df) ÷ 41, 41)), :auto)
2171×41 DataFrame
  Row │ x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 x16 x17 ⋯ │ Float64 Float64 Float64 Float64 Float64 Float64 Float64 Float64 Float64 Float64 Float64 Float64 Float64 Float64 Float64 Float64 Float64 ⋯──────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
    1 │ 122.507 77.8383 95.3854 81.4284 131.8 57.1008 65.8545 84.5781 130.348 155.776 114.96 58.0395 165.353 192.448 129.974 139.679 77.458 ⋯ 2 │ 122.729 78.3159 95.8029 81.7817 132.142 57.2633 65.9749 84.7923 130.557 156.021 115.185 58.1356 165.709 192.901 130.176 139.793 77.5758    
    3 │ 122.952 238.488 96.2481 82.1657 132.5 57.4258 66.1241 85.0073 130.766 156.281 115.416 58.2317 166.072 193.387 130.38 139.906 77.6937    
    4 │ 123.18 238.834 96.7181 82.5991 132.858 57.5883 66.281 85.2519 130.976 156.542 115.646 58.3278 166.458 193.882 130.584 140.019 77.8115    
  ⋮ │ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋱ 2168 │ 76.4467 93.8315 80.2422 130.655 56.6275 65.3732 83.7593 129.567 154.81 114.134 57.7004 164.007 190.775 129.211 139.227 76.9876 142.758 ⋯ 2169 │ 76.742 94.2027 80.514 130.872 56.7377 65.4935 83.9443 129.752 155.046 114.34 57.7839 164.336 191.168 129.401 139.34 77.1044 142.952     
 2170 │ 77.0497 94.5865 80.7859 131.137 56.8479 65.6139 84.1498 129.937 155.289 114.547 57.8673 164.666 191.582 129.592 139.453 77.2223 143.153     
 2171 │ 77.444 94.9704 81.0751 131.468 56.9581 65.7342 84.3639 130.138 155.533 114.753 57.9508 164.996 191.995 129.783 139.566 77.3401 143.355 

```

As you can see above values are being created as  
122.50652071263562 ==\> 1st row  
122.72935583046971 ==\> 2nd row  
122.95219094830381 ==\> 3rd row

I need them in columns x1, x2, x3 …

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [October 26, 2023, 8:35am UTC](https://discourse.julialang.org/t/create-data-frame-using-values-from-vector-as-columns/105416/2 "2023-10-26T08:35:33Z")

</div>

Just transpose your reshape?

```julia
DataFrame(reshape(df.minutes, (nrow(df) ÷ 41, 41))', :auto)

```

(note the `'` after `reshape(...)`)

---

<div class="post-metadata">

**Author:** ![Sandy45](https://avatars.discourse-cdn.com/v4/letter/s/3ec8ea/32.png) [@Sandy45](https://discourse.julialang.org/u/Sandy45)\
**Post date:** [October 26, 2023, 1:29pm UTC](https://discourse.julialang.org/t/create-data-frame-using-values-from-vector-as-columns/105416/3 "2023-10-26T13:29:22Z")

</div>

@nilshg Yes, that seems to be working. I’ve also tried transpose but seems like had `'` at wrong place. Thank you for help!

---

<div class="post-metadata">

**Author:** ![Sandy45](https://avatars.discourse-cdn.com/v4/letter/s/3ec8ea/32.png) [@Sandy45](https://discourse.julialang.org/u/Sandy45)\
**Post date:** [October 26, 2023, 1:51pm UTC](https://discourse.julialang.org/t/create-data-frame-using-values-from-vector-as-columns/105416/4 "2023-10-26T13:51:35Z")

</div>

Sorry, but above logic is creating 2171 columns where I want only 41 columns.

---

<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:** [October 26, 2023, 2:11pm UTC](https://discourse.julialang.org/t/create-data-frame-using-values-from-vector-as-columns/105416/5 "2023-10-26T14:11:47Z")

</div>

Try:  
`DataFrame(reshape(df.minutes, 41, nrow(df) ÷ 41)', :auto)`

---

<div class="post-metadata">

**Author:** ![Sandy45](https://avatars.discourse-cdn.com/v4/letter/s/3ec8ea/32.png) [@Sandy45](https://discourse.julialang.org/u/Sandy45)\
**Post date:** [October 26, 2023, 2:21pm UTC](https://discourse.julialang.org/t/create-data-frame-using-values-from-vector-as-columns/105416/6 "2023-10-26T14:21:49Z")

</div>

That worked - Thank You!

---

<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:** [October 26, 2023, 9:00pm UTC](https://discourse.julialang.org/t/create-data-frame-using-values-from-vector-as-columns/105416/7 "2023-10-26T21:00:19Z")

</div>

It may be useful to take advantage of reshape’s ability to calculate undefined slice

```julia
dfminutes=rand(41*173)

julia> reshape(dfminutes, 41, :)
41×173 Matrix{Float64}:
 0.561983 0.889694 … 0.162916 0.648989
 0.51669 0.753867 0.18693 0.613083    
 0.712268 0.733308 0.13452 0.176377 

....

dfminutes=rand(4*5*7)
julia> reshape(dfminutes, 4, :, 5)
4×7×5 Array{Float64, 3}:
[:, :, 1] =
 0.210488 0.0881805 … 0.589609 0.70231
 0.0110701 0.114932 0.284197 0.315759
 0.515938 0.359342 0.474205 0.125336
 0.311296 0.676618 0.17541 0.323624

[:, :, 2] =
 0.575646 0.407617 … 0.3827 0.846628

```

As an alternative to the transpose function, the permutedims() function is available which also allows you to act on the name of the resulting columns
