You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Julia中用PrettyTables与DataFrames将表头值设为第一列

Julia中设置-1到1为第一列的实现方法

DataFrames替换默认序号列

要把[-1, -3/4, -1/2, -1/4, 0, 1/4, 1/2, 3/4, 1]作为第一列替换默认序号,可按以下两种方式操作:

  • 构造DataFrame时直接加入索引列
    先定义目标索引数组,再和数据AStrings组合:
    header_vals = [-1, -3/4, -1/2, -1/4, 0, 1/4, 1/2, 3/4, 1]
    # 单列数据场景
    df = DataFrame(索引=header_vals, 数据=AStrings)
    # 多列数据场景,自动生成列名
    df = DataFrame([header_vals AStrings], :auto)
    
  • 给现有DataFrame插入第一列
    若已创建好包含AStrings的DataFrame,用insertcols!在最前面插入索引列:
    header_vals = [-1, -3/4, -1/2, -1/4, 0, 1/4, 1/2, 3/4, 1]
    df = DataFrame(AStrings, [:("-1"), :("-3/4"), :("-1/2"), :("-1/4"), :("0"), :("1/4"), :("1/2"), :("3/4"), :("1")])
    insertcols!(df, 1, :索引 => header_vals)
    

PrettyTables设置第一列为目标值

要让第一列显示目标数值,需先将索引值与数据合并为带索引列的表格,再输出:

header_vals = [-1, -3/4, -1/2, -1/4, 0, 1/4, 1/2, 3/4, 1]
# 合并索引列和原始数据
table_data = hcat(header_vals, AStrings)
# 自定义表头(可选),第一列标注为"索引"
pretty_table(table_data, header = ["索引", "列1", "列2", "列3", "列4", "列5", "列6", "列7", "列8", "列9"])
# 无需自定义表头时直接输出
pretty_table(table_data)

内容的提问来源于stack exchange,提问作者Hana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 04:55:21