Python 当使用to_HTML将数据帧转换为HTML时,如何更改数据帧的数据类型?

Python 当使用to_HTML将数据帧转换为HTML时,如何更改数据帧的数据类型?,python,html,pandas,Python,Html,Pandas,由于pandas数据框(或系列)的列的类型是均匀的,除非我添加dtype='object',我想知道在使用pandas.DataFrame.to_html()时如何更改某些行的dtype。例如,我希望第1-3行显示为int,而第4-5行显示为float。从文档到html,我知道我有一些选项,比如CSS样式。但是,我不知道如何使用该函数实现它 import pandas as pd df = pd.DataFrame({'Column1': [2, 3, 4, 5, 6.0], 'Column2'

由于pandas数据框(或系列)的列的类型是均匀的,除非我添加dtype='object',我想知道在使用pandas.DataFrame.to_html()时如何更改某些行的dtype。例如,我希望第1-3行显示为int,而第4-5行显示为float。从文档到html,我知道我有一些选项,比如CSS样式。但是,我不知道如何使用该函数实现它

import pandas as pd
df = pd.DataFrame({'Column1': [2, 3, 4, 5, 6.0],
'Column2': [2, 3, 3, 2, 1.0]})
这给了我一个例子:

Column1 Column2
0   2.0 2.0
1   3.0 3.0
2   4.0 3.0
3   5.0 2.0
4   6.0 1.0
所以我想以某种方式使用这个函数(to_html),以便

df_html = df.to_html(*options*)
from IPython.display import HTML
HTML(df_html)

Column1 Column2
0   2   2
1   3   3
2   4   3
3   5.0 2.0
4   6.0 1.0

这将起作用,尽管它有点特别:

df  = df.astype(str)
sty = lambda x : str(int(float(x)))
for col in df.columns:
    df.loc[df.index<3, col] = df.loc[df.index<3, col].apply(sty)
df=df.astype(str)
sty=lambda x:str(int(float(x)))
对于df.列中的列:
df.loc[df.index