Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Row bind Julia data frames

How do I append one data frame to another, akin to SQL's union or R's rbind?

Say I have data frames A and B defined as follows.

A = DataFrame(x = [1, 2, 3], y = [4, 5, 6])
B = DataFrame(x = [4, 5, 6], y = [7, 8, 9])

One way to approach this would be as follows:

C = deepcopy(A)

for i = 1:size(B, 1)
    push!(C, Array(B[i,:]))
end

While this works, it feels a little hacky to me. Is there a better or more idiomatic way to do this?

like image 373
Alex A. Avatar asked Dec 18 '15 03:12

Alex A.


1 Answers

Array concatenation [A;B] is the simplest way to add rows of one DataFrame to another:

julia> A = DataFrame(x = [1, 2, 3], y = [4, 5, 6]);
julia> B = DataFrame(x = [4, 5, 6], y = [7, 8, 9]);
julia> [A;B]
6x2 DataFrames.DataFrame
| Row | x | y |
|-----|---|---|
| 1   | 1 | 4 |
| 2   | 2 | 5 |
| 3   | 3 | 6 |
| 4   | 4 | 7 |
| 5   | 5 | 8 |
| 6   | 6 | 9 | 
like image 165
Reza Afzalan Avatar answered Oct 06 '22 18:10

Reza Afzalan