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, 1/4, 1/2, 3/4, 1]))
CodePudding user response:
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).
CodePudding user response:
If I understand your question correctly, you're asking to insert the names of the columns as the first column of the DataFrame, is that correct?
If so, you can use names
to get the column names, and use insertcols!
to add them as a column:
insertcols!(df, 1, :ColNames => names(df))
For eg.
julia> DataFrame(rand(UInt8, 9, 9), [:("-1"), :("-3/4") , :("-1/2"), :("-1/4"), :("0"), :("1/4"), :("1/2"), :("3/4"),:("1")])
9×9 DataFrame
Row │ -1 -3/4 -1/2 -1/4 0 1/4 1/2 3/4 1
│ UInt8 UInt8 UInt8 UInt8 UInt8 UInt8 UInt8 UInt8 UInt8
─────┼───────────────────────────────────────────────────────────────
1 │ 49 110 218 249 10 242 68 235 146
2 │ 134 239 191 166 158 128 74 144 162
3 │ 38 5 222 254 81 216 157 77 145
4 │ 168 12 180 101 239 235 107 147 81
5 │ 201 143 54 77 212 130 176 108 134
6 │ 42 5 27 117 11 162 84 184 118
7 │ 244 86 206 92 44 96 121 241 23
8 │ 196 245 138 59 215 44 68 225 75
9 │ 161 42 55 236 61 107 254 244 34
julia> insertcols!(df, 1, :MyColumnNames => names(df))
9×10 DataFrame
Row │ MyColumnNames -1 -3/4 -1/2 -1/4 0 1/4 1/2 3/4 1
│ String UInt8 UInt8 UInt8 UInt8 UInt8 UInt8 UInt8 UInt8 UInt8
─────┼──────────────────────────────────────────────────────────────────────────────
1 │ -1 49 110 218 249 10 242 68 235 146
2 │ -3/4 134 239 191 166 158 128 74 144 162
3 │ -1/2 38 5 222 254 81 216 157 77 145
4 │ -1/4 168 12 180 101 239 235 107 147 81
5 │ 0 201 143 54 77 212 130 176 108 134
6 │ 1/4 42 5 27 117 11 162 84 184 118
7 │ 1/2 244 86 206 92 44 96 121 241 23
8 │ 3/4 196 245 138 59 215 44 68 225 75
9 │ 1 161 42 55 236 61 107 254 244 34