【问题标题】:Format rows in styled dataframe?格式化样式数据框中的行?
【发布时间】:2021-08-11 18:01:25
【问题描述】:

y=TableA.reset_index().rename({'index':''},axis=1)
format_two={ y.columns[1]: "{0:,.2f}",
           y.columns[2]: "{0:,.2f}",
          }

xx = y.style.background_gradient(cmap='Blues',axis=1).format(format_two).hide_index()

我正在尝试使前两行具有带 0 个尾随小数的 int 类型。但是努力将行传递给 format_two 变量。我该怎么办?

【问题讨论】:

    标签: python pandas dataframe data-visualization sklearn-pandas


    【解决方案1】:

    尝试:

    f = lambda x: f'{int(x):,d}' if int(x) == x else f'{x:,.2f}'
    format_two={ y.columns[1]: f,
                 y.columns[2]: "{0:,.2f}",
               }
    

    输出(注意我只格式化第一列):

    【讨论】:

    • 难以置信。 TYSM。
    猜你喜欢
    • 1970-01-01
    • 2020-08-03
    • 2019-09-19
    • 2014-06-11
    • 1970-01-01
    • 2018-01-11
    • 2019-03-17
    • 2012-09-14
    • 2016-12-20
    相关资源
    最近更新 更多