Search code examples
dataframevectorjulia

For df in vector creates vector, but I want a dataframe


I am using for dfx in MyDFvector to create a variable, but it is returning Vector[Vector] but I want just a DataFrame. Any help?

recreate minimal problem

using DataFrames

# create some DataFrames
mydfbase = DataFrame(a=[41, 41, 41, 41, 41, 41])
mydf1 = DataFrame(a=1:6)
mydf2 = DataFrame(a=7:12)
mydf3 = DataFrame(a=13:18)

# put into a vector
MyDFvector = [mydf1, mydf2, mydf3]
println(typeof(MyDFvector))

# 
RawDiff = [mydfbase.a  .-  dfx.a for dfx in MyDFvector]
println(typeof(RawDiff))
println(RawDiff)

mydfWant = DataFrame(a=[40, 39, 38, 37, 36, 35], b=[34, 33, 32, 31, 30, 29], c=[28, 27, 26, 25, 24, 23])
println(typeof(mydfWant))
println(mydfWant)

'= = = = = = = = = = = Terminal output
What I got-->
Vector{DataFrame}
Vector{Vector{Int64}}
[[40, 39, 38, 37, 36, 35], [34, 33, 32, 31, 30, 29], [28, 27, 26, 25, 24, 23]]

What I Want-->  (any generated column names)
DataFrame
6×3 DataFrame
 Row │ a      b      c     
     │ Int64  Int64  Int64 
─────┼─────────────────────
   1 │    40     34     28
   2 │    39     33     27
   3 │    38     32     26
   4 │    37     31     25
   5 │    36     30     24
   6 │    35     29     23

Solution

  • Seems the following will get you to mydfWant:

    julia> DataFrame(stack(RawDiff), :auto)
    6×3 DataFrame
     Row │ x1     x2     x3    
         │ Int64  Int64  Int64 
    ─────┼─────────────────────
       1 │    40     34     28
       2 │    39     33     27
       3 │    38     32     26
       4 │    37     31     25
       5 │    36     30     24
       6 │    35     29     23
    

    For a lot of useful help in Julia, pressing '?' in the REPL and then a function or a type is very helpful (for example the function stack in this answer).