我想添加與 Julia 中的第一列相同的標題值(從 -1 到 1)。我嘗試過漂亮的表格,但我只能將其添加為行標題。在資料幀中,我想將序列號替換為范圍(從-1到1)圖片和代碼已附加。
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]))
uj5u.com熱心網友回復:
看起來資料實際上更像是一個矩陣而不是資料框,并且添加另一列具有不同性質的含義并不好。如果這個想法只是為了漂亮地列印這個矩陣,也許以下就足夠了:
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 │
└───────┴─────────────┴─────────────┴─────────────┴─────────────┴─────────────┘
OP 提到 9 列和名稱提前 1/4,但它是相同的想法(5 列適合 stackoverflow 寬度)。
uj5u.com熱心網友回復:
如果我正確理解您的問題,您要求將列的名稱作為 DataFrame 的第一列插入,對嗎?
如果是這樣,您可以使用names獲取列名,insertcols!并將它們添加為列:
insertcols!(df, 1, :ColNames => names(df))
例如。
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/505724.html
上一篇:資料inpd.DataFrame
下一篇:左加入但保留右側資料框中的所有列
