5

I have a pandas dataframe with three columns of which the first is the Date. To produce a html output I use this:

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

how is it possible to specifiy the format of the Date as well...something like:

html_string = df.style.format({'Date': "%Y.%m",'second': "{:0,.0f}",third: "{:0,.0f}"}).set_table_styles(styles).render()
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
y4nnick
  • 101
  • 1
  • 4

3 Answers3

3

You can do:

html_string = df.style.format({'Date': "{:%Y.%m}",
                               'second': "{:0,.0f}",
                               'third': "{:0,.0f}"}).set_table_styles(styles).render()
mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
2

Actually it takes a string as argument...so this should work:

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

thanks though

y4nnick
  • 101
  • 1
  • 4
1

For me works:

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)

Sample:

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>
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Unfortunately I think it is not supported yet... But maybe I am wrong, try post question. – jezrael Oct 05 '17 at 14:12
  • Alright thanks. I've been trying to figure it out for about a day now... and I can't seem to. Seems like it isn't supported to me too. As of now it looks like if you have a datetime index and you only care about the date part, you're going to be stuck with looking at the time part too. – Alex Luis Arias Oct 05 '17 at 14:30
  • Unfortunately :( – jezrael Oct 05 '17 at 14:30
  • Here's the question I posted in case you wanted to follow: https://stackoverflow.com/questions/46588792/formatting-datetime-index-with-pandas-dataframe-style – Alex Luis Arias Oct 05 '17 at 15:17