# Writing data to csv

**URL:** <https://discourse.julialang.org/t/writing-data-to-csv/59670>\
**Category:** Data\
**Created:** [April 20, 2021, 2:48pm UTC](https://discourse.julialang.org/t/writing-data-to-csv/59670 "2021-04-20T14:48:25Z")\
**Posts on this page:** 8\
**Page:** 2

<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:** [April 26, 2021, 1:27pm UTC](https://discourse.julialang.org/t/writing-data-to-csv/59670/21 "2021-04-26T13:27:51Z")

</div>

No, I want to see the actual matrix, what it looks like when printed at the REPL. Or a subset of it if it’s too big.

---

<div class="post-metadata">

**Author:** ![ashwanimalviya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ashwanimalviya/32/22716_2.png) [@ashwanimalviya](https://discourse.julialang.org/u/ashwanimalviya)\
**Post date:** [April 26, 2021, 1:31pm UTC](https://discourse.julialang.org/t/writing-data-to-csv/59670/22 "2021-04-26T13:31:24Z")

</div>

Here it is @pdeffebach

```julia
5×76 DataFrame
 Row │ x1 x2 x3 x4 x5 x6 x7 ⋯
     │ Any Any Any Any Any Any Any ⋯
─────┼──────────────────────────────────────────────────────────────────────────
   1 │ FC211 FC645 FC6325 FC6328 FC122 FC221 FC212 ⋯
   2 │ -0.1 -19.5536 -24.0226 -26.1236 -26.3944 -28.3969 -28.7
   3 │ 0.0 19.5536 24.0226 26.1236 26.3944 28.3969 28.79
   4 │ 10.0 8.2803 7.88725 7.70247 7.67866 7.50255 7.467
   5 │ 0.0274848 0.0227582 0.0216779 0.0211701 0.0211046 0.0206206 0.020 ⋯
                                                              70 columns omitted

```

without converting into `DataFrames` it looks like

```julia
5×76 Array{Any,2}:
   "FC211" "FC645" "FC6325" … "FC6414" "FC531"
 -0.1 -19.5536 -24.0226 -30.9518 -29.4389
  0.0 19.5536 24.0226 94.6948 102.333
 10.0 8.2803 7.88725 1.67175 1.0
  0.0274848 0.0227582 0.0216779 0.00459478 0.00274848

```

---

<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:** [April 26, 2021, 1:35pm UTC](https://discourse.julialang.org/t/writing-data-to-csv/59670/23 "2021-04-26T13:35:14Z")

</div>

And how does it print out to CSV?

You aren’t constructing your DataFrames very well. As you can see, the column names are treated as data. Do `df = DataFrame(K2Score[2:end, :], K2Score[1,:]); CSV.write("myfilename.csv", df)` and see what changes.

---

<div class="post-metadata">

**Author:** ![ashwanimalviya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ashwanimalviya/32/22716_2.png) [@ashwanimalviya](https://discourse.julialang.org/u/ashwanimalviya)\
**Post date:** [April 26, 2021, 1:38pm UTC](https://discourse.julialang.org/t/writing-data-to-csv/59670/24 "2021-04-26T13:38:17Z")

</div>

I need the column name as data for further analysis, since they are codified purposly.

---

<div class="post-metadata">

**Author:** ![ashwanimalviya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ashwanimalviya/32/22716_2.png) [@ashwanimalviya](https://discourse.julialang.org/u/ashwanimalviya)\
**Post date:** [April 26, 2021, 1:40pm UTC](https://discourse.julialang.org/t/writing-data-to-csv/59670/25 "2021-04-26T13:40:41Z")

</div>

But my problem is not column names, the problem happening in the row below it.

---

<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:** [April 26, 2021, 1:41pm UTC](https://discourse.julialang.org/t/writing-data-to-csv/59670/26 "2021-04-26T13:41:53Z")

</div>

They will still be printed. Did you run the code I gave you? You will see that the column names are in the DataFrame.

It’s difficult to help you when you don’t give the information we need to diagnose the problem.

---

<div class="post-metadata">

**Author:** ![ashwanimalviya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ashwanimalviya/32/22716_2.png) [@ashwanimalviya](https://discourse.julialang.org/u/ashwanimalviya)\
**Post date:** [April 26, 2021, 2:03pm UTC](https://discourse.julialang.org/t/writing-data-to-csv/59670/27 "2021-04-26T14:03:07Z")

</div>

Sorry @pdeffebach for not being very clear with my doubt.

I ran your suggested code like

```julia
Result = [permutedims(Names); K2Score; ShortestPath; score; cum_weight;]
# Making heirarchy by arranging the result in ascending order.
xs = Result
Result = sortslices(xs; dims=2, by=x->x[3])
# Analysis finshed, time to export result.
# the ´Result´ is my desired output needs to be written in csv.
df = DataFrame(Result[2:end, :], Result[1,:]) 
CSV.write("myfilename.csv", df)

```

I hope it is correct. but I am getting following error while running it.

```julia
MethodError: no method matching DataFrame(::Array{Any,2}, ::Array{Any,1})
Closest candidates are:
  DataFrame(::AbstractArray{T,2} where T) at deprecated.jl:70
  DataFrame(::AbstractArray{T,2} where T, ::AbstractArray{Symbol,1}; makeunique) at C:\Users\tecnico2\.julia\packages\DataFrames\3mEXm\src\dataframe\dataframe.jl:322
  DataFrame(::AbstractArray{T,2} where T, ::AbstractArray{var"#s45",1} where var"#s45"<:AbstractString; makeunique) at C:\Users\tecnico2\.julia\packages\DataFrames\3mEXm\src\dataframe\dataframe.jl:327
  ...

Stacktrace:
 [1] top-level scope at In[127]:6
 [2] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091

```

I guess the resean behind , `Result[1, :]` is `76-element Array{Any,1}:` and `Result[2:end, :]` is` 4×76 Array{Any,2}:`

I am sorry again if I made you pissed of.

Thanks for taking time to write.

---

<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:** [April 26, 2021, 2:11pm UTC](https://discourse.julialang.org/t/writing-data-to-csv/59670/28 "2021-04-26T14:11:01Z")

</div>

Sorry, i missed that you have to convert the names to a vector of `String`s. Here is a full MWE that should help you construct a DataFrame and write to a CSV without problems.

```julia
julia> using DataFrames, CSV

julia> x = [ "FC211" "FC645" "FC6325" 
       -0.1 -19.5536 -24.0226    
        0.0 19.5536 24.0226    
       10.0 8.2803 7.88725   
        0.0274848 0.0227582 0.0216779]
5×3 Matrix{Any}:
   "FC211" "FC645" "FC6325"
 -0.1 -19.5536 -24.0226
  0.0 19.5536 24.0226
 10.0 8.2803 7.88725
  0.0274848 0.0227582 0.0216779

julia> nms = string.(x[1,:]);

julia> vals = Float64.(x[2:end, :]);

julia> df = DataFrame(vals, nms)
4×3 DataFrame
 Row │ FC211 FC645 FC6325      
     │ Float64 Float64 Float64     
─────┼──────────────────────────────────────
   1 │ -0.1 -19.5536 -24.0226
   2 │ 0.0 19.5536 24.0226
   3 │ 10.0 8.2803 7.88725
   4 │ 0.0274848 0.0227582 0.0216779

julia> io = IOBuffer();

julia> CSV.write(io, df);

julia> csv_str = (String(take!(io)));

julia> println(csv_str)
FC211,FC645,FC6325
-0.1,-19.5536,-24.0226
0.0,19.5536,24.0226
10.0,8.2803,7.88725
0.0274848,0.0227582,0.0216779

```

[Previous page](https://discourse.julialang.org/t/writing-data-to-csv/59670.md?page=1)
