# Output sparse matrix to csv

**URL:** <https://discourse.julialang.org/t/output-sparse-matrix-to-csv/67513>\
**Category:** General Usage\
**Tags:** question, csv, sparse\
**Created:** [September 1, 2021, 3:31pm UTC](https://discourse.julialang.org/t/output-sparse-matrix-to-csv/67513 "2021-09-01T15:31:42Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nenndul](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nenndul/32/22149_2.png) [@Nenndul](https://discourse.julialang.org/u/Nenndul)\
**Post date:** [September 1, 2021, 3:31pm UTC](https://discourse.julialang.org/t/output-sparse-matrix-to-csv/67513/1 "2021-09-01T15:31:42Z")

</div>

I have a little program that correctly produces a sparse matrix from data. When I call the result:

`display(dataOut)`

I get the expected:

> 3×3 SparseMatrixCSC{Float64, Int64} with 3 stored entries:  
> -1.0 -1.0 ⋅  
> -1.23 ⋅ ⋅  
> ⋅ ⋅ ⋅

Now I want to export that to a .csv file using

`writedlm("Output.csv", dataOut, ",")`

…but it fills the blank data with zeros.

Is there any way to avoid this behavior?

[Edit: Just to clarify, I am looking for a csv file output that looks exactly like the SparseMatrix, i.e. with blanks in the empty cells and observing the rows and columns]

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [September 1, 2021, 5:06pm UTC](https://discourse.julialang.org/t/output-sparse-matrix-to-csv/67513/2 "2021-09-01T17:06:14Z")

</div>

See solution in this [post](https://discourse.julialang.org/t/saving-sparse-matrix/28089/2).

---

<div class="post-metadata">

**Author:** ![Nenndul](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nenndul/32/22149_2.png) [@Nenndul](https://discourse.julialang.org/u/Nenndul)\
**Post date:** [September 1, 2021, 5:13pm UTC](https://discourse.julialang.org/t/output-sparse-matrix-to-csv/67513/3 "2021-09-01T17:13:25Z")

</div>

Thanks for the suggestion.

Using dataframes like this doesn’t produce the correct behavior I’m afraid:

```julia
df = DataFrame([dataOut])

CSV.write("Output.csv", df)

```

I’m not sure if I am doing something wrong here, but I specifically want the data to be “clean”, as it were. So I don’t want a dataframe with any headers or anything else but the non-zero values one value per cell.

---

<div class="post-metadata">

**Author:** ![Nenndul](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nenndul/32/22149_2.png) [@Nenndul](https://discourse.julialang.org/u/Nenndul)\
**Post date:** [September 1, 2021, 6:05pm UTC](https://discourse.julialang.org/t/output-sparse-matrix-to-csv/67513/5 "2021-09-01T18:05:12Z")

</div>

Still doing it, I’m afraid. I tried a couple of variations.

When I try your code directly it throws the following:

> ERROR: LoadError: ArgumentError: column name :V not found in the data frame; existing most similar names are: :m and :n

---

<div class="post-metadata">

**Author:** ![Nenndul](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nenndul/32/22149_2.png) [@Nenndul](https://discourse.julialang.org/u/Nenndul)\
**Post date:** [September 1, 2021, 6:15pm UTC](https://discourse.julialang.org/t/output-sparse-matrix-to-csv/67513/7 "2021-09-01T18:15:45Z")

</div>

Ah, that runs now, not sure what I did wrong.

However, now it is not observing the rows and columns, just outputting everything in one row in the .csv

[Will edit previous posts for formatting, thanks for the heads up]

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [September 1, 2021, 7:00pm UTC](https://discourse.julialang.org/t/output-sparse-matrix-to-csv/67513/8 "2021-09-01T19:00:45Z")

</div>

Ok, could you try this:

```julia
using SparseArrays, DelimitedFiles
M = sprand(5, 5, 0.5)
Md = Matrix(M) # creates dense Matrix{Float64}
Mb = similar(Md,Any) # similar Matrix{Any}, type Any allows mixing floats with strings
Mb .= Md # assigns content of Md to Mb
Mb[Md .== 0] .= "" # bitmatrix Md.==0 provides indices of all 0's
open("spmatrix_blanks.csv", "w") do io
    writedlm(io, Mb, ',')
end

```

---

<div class="post-metadata">

**Author:** ![Nenndul](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nenndul/32/22149_2.png) [@Nenndul](https://discourse.julialang.org/u/Nenndul)\
**Post date:** [September 1, 2021, 7:27pm UTC](https://discourse.julialang.org/t/output-sparse-matrix-to-csv/67513/9 "2021-09-01T19:27:24Z")

</div>

Perfect. Thanks!

Now I’m going to have to spend the rest of the day trying to figure out why that works 😀

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [September 1, 2021, 7:45pm UTC](https://discourse.julialang.org/t/output-sparse-matrix-to-csv/67513/10 "2021-09-01T19:45:27Z")

</div>

Added some comments to the code, in case it helps.

---

<div class="post-metadata">

**Author:** ![Nenndul](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nenndul/32/22149_2.png) [@Nenndul](https://discourse.julialang.org/u/Nenndul)\
**Post date:** [September 1, 2021, 7:48pm UTC](https://discourse.julialang.org/t/output-sparse-matrix-to-csv/67513/11 "2021-09-01T19:48:17Z")

</div>

Thanks!

So basically, it’s a dense matrix with blanks instead of zeros?

If so, that might actually be more useful to me in the long run.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [September 1, 2021, 7:54pm UTC](https://discourse.julialang.org/t/output-sparse-matrix-to-csv/67513/12 "2021-09-01T19:54:25Z")

</div>

Yes, it is a weird dense matrix of Any which was made up just to satisfy your CSV output request. Cannot see any other use for it, honestly speaking.

---

<div class="post-metadata">

**Author:** ![Nenndul](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nenndul/32/22149_2.png) [@Nenndul](https://discourse.julialang.org/u/Nenndul)\
**Post date:** [September 1, 2021, 7:59pm UTC](https://discourse.julialang.org/t/output-sparse-matrix-to-csv/67513/13 "2021-09-01T19:59:51Z")

</div>

At the moment I’m just using Excel’s conditional formatting on it, otherwise I have to run a macro to get rid of the zeros on the other end. Eventually this step will probably be redundant if I can get the whole thing to run in its own app, but for now this is great.

Thanks again!

---

<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:** [September 1, 2021, 8:18pm UTC](https://discourse.julialang.org/t/output-sparse-matrix-to-csv/67513/14 "2021-09-01T20:18:28Z")

</div>

`Tables.table(mat)` will wrap a matrix in a tables-compatible object for use with `CSV.write`.

As for zeros versus `missing` for the sparse entries, I think there is a package for that, but googling hasn’t gotten me there. But if you can make a sparse matrix where the zero-values are `missing`, you can pass the option `missingstring = "."` to `CSV.write` and you will be good.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [September 1, 2021, 8:26pm UTC](https://discourse.julialang.org/t/output-sparse-matrix-to-csv/67513/15 "2021-09-01T20:26:56Z")

</div>

If you don’t need an ASCII file, perhaps the `DataDrop` package would work for you?

```julia
julia> using DataDrop
   
julia> using SparseArrays  
   
julia> c = sprand(4, 3, 0.5)     
4×3 SparseMatrixCSC{Float64, Int64} with 5 stored entries:     
  ⋅ ⋅ 0.780225   
  ⋅ 0.0612236 ⋅   
  ⋅ ⋅ ⋅   
 0.262007 0.778962 0.0651033  
   
julia> DataDrop.store_matrix("c.h5", c)   
   
julia> yac = DataDrop.retrieve_matrix("c.h5")   
4×3 SparseMatrixCSC{Float64, Int64} with 5 stored entries:     
  ⋅ ⋅ 0.780225   
  ⋅ 0.0612236 ⋅   
  ⋅ ⋅ ⋅   
 0.262007 0.778962 0.0651033  

```

---

<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:** [September 1, 2021, 8:30pm UTC](https://discourse.julialang.org/t/output-sparse-matrix-to-csv/67513/16 "2021-09-01T20:30:55Z")

</div>

OP is re-opening their file in Excel, so I think this is a bit heavy.

---

<div class="post-metadata">

**Author:** ![Nenndul](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nenndul/32/22149_2.png) [@Nenndul](https://discourse.julialang.org/u/Nenndul)\
**Post date:** [September 1, 2021, 9:04pm UTC](https://discourse.julialang.org/t/output-sparse-matrix-to-csv/67513/17 "2021-09-01T21:04:33Z")

</div>

I see. This might be useful later on, but the output file is the same size as the CSV. Is this correct? Is there an advantage to using this format?

---

<div class="post-metadata">

**Author:** ![Nenndul](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nenndul/32/22149_2.png) [@Nenndul](https://discourse.julialang.org/u/Nenndul)\
**Post date:** [September 1, 2021, 9:09pm UTC](https://discourse.julialang.org/t/output-sparse-matrix-to-csv/67513/18 "2021-09-01T21:09:46Z")

</div>

I must confess I can’t get that to work.

I’m assuming “mat” is the sparse matrix? So for my example:

`Tables.table(dataOut)`

?

---

<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:** [September 1, 2021, 9:12pm UTC](https://discourse.julialang.org/t/output-sparse-matrix-to-csv/67513/19 "2021-09-01T21:12:22Z")

</div>

Yes. Does that work?

You may need to add Tables.jl

---

<div class="post-metadata">

**Author:** ![Nenndul](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nenndul/32/22149_2.png) [@Nenndul](https://discourse.julialang.org/u/Nenndul)\
**Post date:** [September 1, 2021, 9:14pm UTC](https://discourse.julialang.org/t/output-sparse-matrix-to-csv/67513/20 "2021-09-01T21:14:44Z")

</div>

Afraid not. It’s throwing:

> ERROR: LoadError: ArgumentError: a ‘SparseMatrixCSC{Float64, Int64}’ is not a table;

---

<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:** [September 1, 2021, 9:19pm UTC](https://discourse.julialang.org/t/output-sparse-matrix-to-csv/67513/21 "2021-09-01T21:19:08Z")

</div>

Works for me

```julia
julia> using Tables, SparseArrays, CSV;

julia> A = sparse([1, 1, 2, 3], [1, 3, 2, 3], [0, 1, 2, 0]);

julia> A_table = Tables.table(A);

julia> CSV.write("my_file.csv", A_table);

```

---

<div class="post-metadata">

**Author:** ![Nenndul](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nenndul/32/22149_2.png) [@Nenndul](https://discourse.julialang.org/u/Nenndul)\
**Post date:** [September 1, 2021, 9:25pm UTC](https://discourse.julialang.org/t/output-sparse-matrix-to-csv/67513/22 "2021-09-01T21:25:25Z")

</div>

Ah yes, that would be me getting the syntax wrong again.

Unfortunately the output is giving me headers and zeros still, even after adding missingstring:

`CSV.write("my_file.csv", A_table, missingstring = ".");`

and with the variation:

`CSV.write("my_file.csv", A_table, missingstring = "");`

[Next page](https://discourse.julialang.org/t/output-sparse-matrix-to-csv/67513.md?page=2)
