Writing data to csv

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.

Here it is @pdeffebach

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

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

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.

2 Likes

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

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

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.

1 Like

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

I ran your suggested code like

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.

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.

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

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
4 Likes