# Convert a dataframe to a 2d array

**URL:** https://discourse.julialang.org/t/convert-a-dataframe-to-a-2d-array/18662
**Category:** New to Julia
**Created:** [December 14, 2018, 4:00pm UTC](https://discourse.julialang.org/t/convert-a-dataframe-to-a-2d-array/18662 "2018-12-14T16:00:32Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Reza\_Mortazavi](https://avatars.discourse-cdn.com/v4/letter/r/ecd19e/32.png) [@Reza\_Mortazavi](https://discourse.julialang.org/u/Reza_Mortazavi)
#### Post date: [December 14, 2018, 4:00pm UTC](https://discourse.julialang.org/t/convert-a-dataframe-to-a-2d-array/18662/1 "2018-12-14T16:00:32Z")

</div>

I am new to Julia. I have an adjacency matrix of a simple graph saved in graph.csv, i.e., (i,j)=1 iff v\_i is linked to v\_j. I want to save only links of the graph, i.e., convert the format to a more efficient and space-saving one since my graph is sparse. My problem is how to convert an y-element Array{Any,1} of links to yx2 Array{Int64,2}.

My initial try is as follows:

graph.csv

```julia
0,1,1,1,1,1,1,1,1,0,1,1,1,1,0,0,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,1,0,0
1,0,1,1,0,0,0,1,0,0,0,0,0,1,0,0,0,1,0,1,0,1,0,0,0,0,0,0,0,0,1,0,0,0
1,1,0,1,0,0,0,1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1,0
1,1,1,0,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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0,0,0,0,0,0,0,0,0,0,0,1,1
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,1,0,0,1,1
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,1,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1
0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1
0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,1
0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,0,0,0,1,1
0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,1,0,0,1,0,1,0,1,1,0,0,0,0,0,1,1,1,0,1
0,0,0,0,0,0,0,0,1,1,0,0,0,1,1,1,0,0,1,1,1,0,1,1,0,0,1,1,1,1,1,1,1,0

```

###########  
code.jl

```julia
Pkg.add("DataFrames.jl");
using DataFrames;
data=readtable("graph.csv",header=false);
data2=[];
for i in 1:size(data,1)
  for j in 1+i:size(data,2)
      if data[i,j]==1
         push!(data2,[i,j])
      end
   end
end

# my problem lies here
writetable("graph2.txt",data2)
reshape(hcat(data2...),size(data2,1),:) # works but is not convinient

```

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [December 14, 2018, 4:14pm UTC](https://discourse.julialang.org/t/convert-a-dataframe-to-a-2d-array/18662/2 "2018-12-14T16:14:40Z")

</div>

Instead of reading a `DataFrame` and converting, I would propose [`DelimitedFiles.readdlm`](https://docs.julialang.org/en/v1/stdlib/DelimitedFiles/#DelimitedFiles.readdlm-Tuple%7BAny,AbstractChar,Type,AbstractChar%7D).

---

<div class="post-metadata">

### Author: ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)
#### Post date: [December 14, 2018, 4:36pm UTC](https://discourse.julialang.org/t/convert-a-dataframe-to-a-2d-array/18662/3 "2018-12-14T16:36:57Z")

</div>

Using `DelimitedFiles` you could do something like this:

```julia
writedlm("graph3.csv", findall(readdlm("graph.csv", ',', Int) .== 1) .|> x -> [x[1],x[2]], ',')

```

---

<div class="post-metadata">

### Author: ![Reza\_Mortazavi](https://avatars.discourse-cdn.com/v4/letter/r/ecd19e/32.png) [@Reza\_Mortazavi](https://discourse.julialang.org/u/Reza_Mortazavi)
#### Post date: [December 14, 2018, 6:57pm UTC](https://discourse.julialang.org/t/convert-a-dataframe-to-a-2d-array/18662/4 "2018-12-14T18:57:48Z")

</div>

Thanks you. It is correct. However, I would like to know how to convert a 1D array of tuples (a,b) to a 2D array i.e., an array of nx2.

---

<div class="post-metadata">

### Author: ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)
#### Post date: [December 14, 2018, 7:03pm UTC](https://discourse.julialang.org/t/convert-a-dataframe-to-a-2d-array/18662/5 "2018-12-14T19:03:12Z")

</div>

Here’s one way:

```julia
julia> tuples = [(1,2), (3,4), (5,6)]
3-element Array{Tuple{Int64,Int64},1}:
 (1, 2)
 (3, 4)
 (5, 6)

julia> vcat((tuples .|> t -> [t[1] t[2]])...)
3×2 Array{Int64,2}:
 1 2
 3 4
 5 6

```

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [December 14, 2018, 10:09pm UTC](https://discourse.julialang.org/t/convert-a-dataframe-to-a-2d-array/18662/6 "2018-12-14T22:09:02Z")

</div>

There’s also nothing wrong with a for loop for readability

```julia
function tuplestomat(x)
    m = length(x[1])
    n = length(x)
    M = Array{typeof(x[1][1])}(undef, m, n)
    for i in 1:n
        for j in 1:m
            M[i, j] = x[i][j]
        end
    end           
return M
end

```
