如何在Jinja中对齐Pandas DataFrame列号文本 [英] How to align Pandas DataFrame column number text in Jinja
问题描述
我通过Jinja将Pandas Dataframe渲染到网页上,但发现数字列保持对齐.
I rendered a Pandas Dataframe to a webpage through Jinja but noticed the number column is left aligned.
当我尝试在特定列上应用以下代码以正确对齐并加载网页时.
When I tried applying the code below on the particular column to align right and loaded the webpage.
df = df.style.set_properties(subset=["col1", "col2"], **{'text-align': 'right'})
在浏览器页面上显示错误.足够有趣的是,在Jupyter Notebook上尝试时,它可以完美运行
It gives an error on the browser page. Funny enough it works perfectly when tried on Jupyter Notebook
TypeError: 'Styler' object is not subscriptable
我想要的是右对齐数字列. 任何人都有更好的解决方案.
What I want is the number column to align right. Anyone has a better solution.
推荐答案
我无法获得有效的Pandas或Jinja解决方案.但是我偶然发现了这个,就解决了整个问题.
I couldn't get a Pandas or Jinja solution that worked. However I stumbled on a this and that solved the whole issue.
这是CSS技巧.我只需要标识特定的列,然后在Style.css文件中应用下面的代码即可.
It was a CSS trick. I simply had to identify the specific column and applied the code below in my Style.css file.
tbody>tr>:nth-child(5){
text-align:right;
}
"5"是列号.
I believe this will be of help to someone.
这篇关于如何在Jinja中对齐Pandas DataFrame列号文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!