【问题标题】:format Date in pandas style render在熊猫样式渲染中格式化日期
【发布时间】:2016-12-02 22:38:55
【问题描述】:

我有一个包含三列的 pandas 数据框,其中第一列是日期。为了产生一个 html 输出,我使用这个:

html_string = df.style.format({'second': "{:0,.0f}",third: "{:0,.0f}"}).set_table_styles(styles).render()

如何也可以指定日期的格式...类似于:

html_string = df.style.format({'Date': "%Y.%m",'second': "{:0,.0f}",third: "{:0,.0f}"}).set_table_styles(styles).render()

【问题讨论】:

    标签: python pandas pandas-styles


    【解决方案1】:

    你可以这样做:

    html_string = df.style.format({'Date': "{:%Y.%m}",
                                   'second': "{:0,.0f}",
                                   'third': "{:0,.0f}"}).set_table_styles(styles).render()
    

    【讨论】:

      【解决方案2】:

      对我有用:

      html_string = df.style.format({'first': lambda x: "{}".format(x.strftime('%Y.%m')) , 
                                     'second': "{:0,.0f}",
                                     'third': "{:0,.0f}"}).set_table_styles('styles').render()
      print (html_string)
      

      示例:

      df = pd.DataFrame({ 'first':['2015-01-05','2015-01-06','2015-01-07'],
                          'second':[4,2.1,5.9],
                         'third':[8,5.1,7.7]})
      df['first'] = pd.to_datetime(df['first'] )
      print (df)
             first  second  third
      0 2015-01-05     4.0    8.0
      1 2015-01-06     2.1    5.1
      2 2015-01-07     5.9    7.7
      

      <table id="T_a301a12e_54ca_11e6_9a6f_acb57da49b4f" None>
      
      
        <thead>
      
          <tr>
      
            <th class="blank">
      
              <th class="col_heading level0 col0">first
      
                <th class="col_heading level0 col1">second
      
                  <th class="col_heading level0 col2">third
      
          </tr>
      
        </thead>
        <tbody>
      
          <tr>
      
            <th id="T_a301a12e_54ca_11e6_9a6f_acb57da49b4f" class="row_heading level2 row0">
              0
      
              <td id="T_a301a12e_54ca_11e6_9a6f_acb57da49b4frow0_col0" class="data row0 col0">
                2015.01
      
                <td id="T_a301a12e_54ca_11e6_9a6f_acb57da49b4frow0_col1" class="data row0 col1">
                  4
      
                  <td id="T_a301a12e_54ca_11e6_9a6f_acb57da49b4frow0_col2" class="data row0 col2">
                    8
      
          </tr>
      
          <tr>
      
            <th id="T_a301a12e_54ca_11e6_9a6f_acb57da49b4f" class="row_heading level2 row1">
              1
      
              <td id="T_a301a12e_54ca_11e6_9a6f_acb57da49b4frow1_col0" class="data row1 col0">
                2015.01
      
                <td id="T_a301a12e_54ca_11e6_9a6f_acb57da49b4frow1_col1" class="data row1 col1">
                  2
      
                  <td id="T_a301a12e_54ca_11e6_9a6f_acb57da49b4frow1_col2" class="data row1 col2">
                    5
      
          </tr>
      
          <tr>
      
            <th id="T_a301a12e_54ca_11e6_9a6f_acb57da49b4f" class="row_heading level2 row2">
              2
      
              <td id="T_a301a12e_54ca_11e6_9a6f_acb57da49b4frow2_col0" class="data row2 col0">
                2015.01
      
                <td id="T_a301a12e_54ca_11e6_9a6f_acb57da49b4frow2_col1" class="data row2 col1">
                  6
      
                  <td id="T_a301a12e_54ca_11e6_9a6f_acb57da49b4frow2_col2" class="data row2 col2">
                    8
      
          </tr>
      
        </tbody>
      </table>

      【讨论】:

      • 不幸的是我认为它还不支持......但也许我错了,请尝试发布问题。
      • 好的,谢谢。我一直试图弄清楚它大约一天了......我似乎无法。好像我也不支持它。到目前为止,如果您有一个日期时间索引并且只关心日期部分,那么您也将被困在查看时间部分。
      • 不幸的是:(
      • 这是我发布的问题,以防您想关注:stackoverflow.com/questions/46588792/…
      【解决方案3】:

      实际上它需要一个字符串作为参数......所以这应该可以工作:

      html_string = df.style.format({'Date': "{:%Y.%m}",'second': "{:0,.0f}",'third': "{:0,.0f}"}).set_table_styles(styles).render()
      

      谢谢

      【讨论】:

      • 这正是我的答案... 17 分钟前
      • 对我来说它不起作用,我使用 python 3。也许尝试使用示例进行测试 df:df = pd.DataFrame({ 'first':['2015-01-05','2015-01-06','2015-01-07'], 'second':[4,2.1,5.9], 'third':[8,5.1,7.7]}) df['first'] = pd.to_datetime(df['first'] ) print (df)
      • @bernie 几乎是:{:"%Y.%m"} vs "{:%Y.%m}" 对我来说只有后者在工作
      • 啊,我看到了@y4nnick。修正了我的例子。
      猜你喜欢
      • 2020-10-20
      • 2019-11-28
      • 2018-09-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-23
      • 1970-01-01
      • 2020-06-11
      相关资源
      最近更新 更多