【问题标题】:Print data frame with columns center-aligned打印列居中对齐的数据框
【发布时间】:2012-10-10 16:59:32
【问题描述】:

我想打印一个列居中对齐的数据框。下面是我尝试过的,我认为打印数据框 test1 会导致列在中心对齐,但事实并非如此。关于如何做到这一点的任何想法?

test=data.frame(x=c(1,2,3),y=c(5,6,7))
names(test)=c('Variable 1','Variable 2')
test[,1]=as.character(test[,1])
test[,2]=as.character(test[,2])
test1=format(test,justify='centre')
print(test,row.names=FALSE,quote=FALSE)
 Variable 1 Variable 2
          1          5
          2          6
          3          7
print(test1,row.names=FALSE,quote=FALSE)
 Variable 1 Variable 2
          1          5
          2          6
          3          7

【问题讨论】:

  • 你是指柱子的中心吗?所以 1、2、3 在Variable 1 中的a(大致)下排列?
  • @TylerRinker 是的,这就是我的意思。
  • @Glen,my answer 是否充分解决了您的问题?如果没有,请添加评论,以便您的问题可以进一步解决。
  • @mrdwab 很好的答案,谢谢。

标签: r printing dataframe formatting center-align


【解决方案1】:

调用这个函数来获得一个数据框来显示:

def pd_centered(df):
    return df.style.set_table_styles([
        {"selector": "th", "props": [("text-align", "center")]},
        {"selector": "td", "props": [("text-align", "center")]}])

例如:

display(pd_centered(original_df))

这将使标题和实际数据单元格居中对齐,如果您愿意,您可以删除tdth 以禁用其中一个。

来源:https://github.com/pandas-dev/pandas/issues/12144

【讨论】:

    【解决方案2】:

    问题在于,为了使其按预期工作,还需要指定“width”参数。

    这是一个例子:

    test.1 <- data.frame(Variable.1 = as.character(c(1,2,3)), 
                         Variable.2 = as.character(c(5,6,7)))
    
    # Identify the width of the widest column by column name
    name.width <- max(sapply(names(test.1), nchar))
    format(test.1, width = name.width, justify = "centre")
    #   Variable.1 Variable.2
    # 1     1          5     
    # 2     2          6     
    # 3     3          7  
    

    但是,这种方法如何处理变量名称长度不同的列?不太好。

    test.2 <- data.frame(A.Really.Long.Variable.Name = as.character(c(1,2,3)), 
                         Short.Name = as.character(c(5,6,7)))
    
    name.width <- max(sapply(names(test.2), nchar))
    format(test.2, width = name.width, justify = "centre")
    #   A.Really.Long.Variable.Name                  Short.Name
    # 1              1                           5             
    # 2              2                           6             
    # 3              3                           7             
    

    当然,有一个解决方法:将每个变量名称的“宽度”更改为相等的长度,方法是用空格填充它们(使用format()

    orig.names <- names(test.2) # in case you want to restore the original names
    names(test.2) <- format(names(test.2), width = name.width, justify = "centre")
    format(test.2, width = name.width, justify = "centre")
    #   A.Really.Long.Variable.Name         Short.Name         
    # 1              1                           5             
    # 2              2                           6             
    # 3              3                           7
    

    【讨论】:

    • 我要破产了sprintf +1
    • 如果列包含字符而不是数字,并且它们比列名本身长,替代方案似乎不起作用?
    猜你喜欢
    • 2012-11-15
    • 1970-01-01
    • 1970-01-01
    • 2019-06-13
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多