I have a list of dataframes with the same columns, how would I stack them on top of each other and make one dataframe?

Hi how’s it going,

I tried using hcat to stack each of these dataframes on top of each other, but it’s not really working as intended. I want to combine a list of dataframes, all with the same column names, into one big dataframe

Thanks

you want vcat, for vertical concatenation as opposed to hcat for horizontal concatenation.

julia> dfs= [DataFrame(rand(5,2)) for i in 1:5]
julia> reduce(vcat, dfs)

If you dataframes don’t have the same columns and you want to stack them on top of one another, filling in missing columns with missings, you can do

julia> dfs= [isodd(i) ? DataFrame(rand(5, 2)) : DataFrame(rand(5, 3)) for i in 1:5]
julia> reduce(vcat, dfs, cols = :union)
4 Likes

or “reduce” typing :slight_smile: with:

df_union = vcat(dfs..., cols = :union)
2 Likes

Thanks guys!