我有一个包含三列的pandas数据框,其中第一列是Date。要生成html输出,我使用:
html_string = df.style.format({'second': "{:0,.0f}",third: "{:0,.0f}"}).set_table_styles(styles).render()
如何指定Date的格式......如:
html_string = df.style.format({'Date': "%Y.%m",'second': "{:0,.0f}",third: "{:0,.0f}"}).set_table_styles(styles).render()
答案 0 :(得分:1)
你可以这样做:
html_string = df.style.format({'Date': "{:%Y.%m}",
'second': "{:0,.0f}",
'third': "{:0,.0f}"}).set_table_styles(styles).render()
答案 1 :(得分:1)
对我而言:
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>
答案 2 :(得分:0)
实际上它需要一个字符串作为参数......所以这应该有效:
html_string = df.style.format({'Date': "{:%Y.%m}",'second': "{:0,.0f}",'third': "{:0,.0f}"}).set_table_styles(styles).render()
谢谢