dataframejuliaprettytable

How to add a column as header in Julia using pretty tables and data frames


I want to add the same header values (from -1 to 1) as the first column in Julia. I have tried pretty tables but I could only add it as row header. In dataframes I want to replace the sequence numbers to the range (from -1 to 1) Pictures and code are attached.

println(DataFrame(AStrings, [:("-1"), :("-3/4") , :("-1/2"), :("-1/4"), :("0"), :("1/4"), :("1/2"), :("3/4"),:("1")]))

pretty_table(AStrings , header = ([-1,-3/4, -1/2, -1/4, 0, enter image description here1/4, 1/2, 3/4, 1]))


Solution

  • Looks like the data is actually more a matrix than a data-frame, and adding another column with a qualitatively different meaning isn't nice. If the idea is just to pretty-print this matrix, perhaps the following is enough:

    julia> using PrettyTables
    
    julia> data = ["$(rand(0:1000)/1000),$(rand(0:1000)/1000)" for i=1:5,j=1:5]
    5×5 Matrix{String}:
     "0.862,0.52"   "0.303,0.367"  "0.747,0.679"  "0.985,0.044"  "0.094,0.463"
     "0.226,0.429"  "0.537,0.598"  "0.625,0.922"  "0.928,0.271"  "0.138,0.162"
     "0.86,0.004"   "0.741,0.861"  "0.747,0.656"  "0.514,0.851"  "0.304,0.555"
     "0.354,0.221"  "0.169,0.824"  "0.431,0.988"  "0.459,0.327"  "0.172,0.614"
     "0.271,0.588"  "0.148,0.942"  "0.842,0.663"  "0.084,0.265"  "0.44,0.818"
    
    julia> names = string.(-1//1:1//2:1//1)
    5-element Vector{String}:
     "-1//1"
     "-1//2"
     "0//1"
     "1//2"
     "1//1"
    
    julia> pretty_table(data; header=names, row_names=names)
    ┌───────┬─────────────┬─────────────┬─────────────┬─────────────┬─────────────┐
    │       │       -1//1 │       -1//2 │        0//1 │        1//2 │        1//1 │
    ├───────┼─────────────┼─────────────┼─────────────┼─────────────┼─────────────┤
    │ -1//1 │  0.862,0.52 │ 0.303,0.367 │ 0.747,0.679 │ 0.985,0.044 │ 0.094,0.463 │
    │ -1//2 │ 0.226,0.429 │ 0.537,0.598 │ 0.625,0.922 │ 0.928,0.271 │ 0.138,0.162 │
    │  0//1 │  0.86,0.004 │ 0.741,0.861 │ 0.747,0.656 │ 0.514,0.851 │ 0.304,0.555 │
    │  1//2 │ 0.354,0.221 │ 0.169,0.824 │ 0.431,0.988 │ 0.459,0.327 │ 0.172,0.614 │
    │  1//1 │ 0.271,0.588 │ 0.148,0.942 │ 0.842,0.663 │ 0.084,0.265 │  0.44,0.818 │
    └───────┴─────────────┴─────────────┴─────────────┴─────────────┴─────────────┘
    

    The OP mentioned 9 columns and names advance by 1/4, but it is the same idea (5 columns fit stackoverflow width).