Create data frame using values from vector as columns

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> 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> 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 …

Just transpose your reshape?

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

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

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

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

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

That worked - Thank You!

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

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

1 Like