Convert dictionary of dataframes into single dataframe

I have a dictionary of dataframes that I want to combine into a single dataframe.

image

This didn’t work.

test2 = hcat(speed_steps)

I think you probably want vcat rather than hcat.

reduce(vcat, values(speed_steps))
3 Likes

pdeffebach,

Yes, that worked. Thanks!

If you, or another reader stumbling across this topic, also want to add the keys to the DataFrame, you can iterate over the keys and values and add the keys as a column to the individual data frames. For example,

julia> A = DataFrame(TimeInterval = [1])
1Γ—1 DataFrame
 Row β”‚ TimeInterval
     β”‚ Int64
─────┼──────────────
   1 β”‚            1

julia> B = DataFrame(TimeInterval = [2])
1Γ—1 DataFrame
 Row β”‚ TimeInterval
     β”‚ Int64
─────┼──────────────
   1 β”‚            2

julia> speeds = Dict(0.3 => A, 0.9 => B)
Dict{Float64, DataFrame} with 2 entries:
  0.3 => 1Γ—1 DataFrame…
  0.9 => 1Γ—1 DataFrame…

julia> for (speed, df) in zip(keys(speeds), values(speeds))
       df[!, :speed] .= speed
       end;

julia> vcat(values(speeds)...)
2Γ—2 DataFrame
 Row β”‚ TimeInterval  speed
     β”‚ Int64         Float64
─────┼───────────────────────
   1 β”‚            1      0.3
   2 β”‚            2      0.9
1 Like

rikh,

Thanks. I probably will eventually need to do that. I’m making the jump from Python. There’s been a slight learning curve for me.

1 Like