use*_*964 8 python pandas jupyter-notebook
例如,定义
df=pd.DataFrame(np.random.randint(0,10,(6,6)))
df
Run Code Online (Sandbox Code Playgroud)
这在 Jupyter 笔记本中给出了以下显示
我的问题是是否可以向数据框添加列分隔符,例如
感谢您的所有答案,目前我使用以下自定义函数
def css_border(x,pos):
return ["border-left: 1px solid red" if i in pos else "border: 0px" for i, col in enumerate(x)]
def display_df_with_delimiter(df,pos):
return df.style.apply(partial(css_border,pos=pos), axis=1)
Run Code Online (Sandbox Code Playgroud)
和
display_df_with_delimiter(df,[0,1,2,5])
Run Code Online (Sandbox Code Playgroud)
给
这段代码应该将所需的行添加到表中。
from IPython.display import display, HTML
CSS = """
.rendered_html td:nth-child(even) {
border-left: 1px solid red;
}
"""
HTML('<style>{}</style>'.format(CSS))
Run Code Online (Sandbox Code Playgroud)
请注意,您可以通过简单地更改border-left属性定义来更改这些线的样式,即使border-left: 2px solid green线条更粗和更绿。
这是演示输出的快照。
尝试pandas.style.apply(...):
from IPython.core.display import display, HTML
def css_border(x):
return ["border-left: 1px solid red" if (i%2==0) else "border: 0px" for i, col in enumerate(x)]
html = (
df.style.apply(css_border, axis=1).render()
)
display(HTML(html))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1748 次 |
| 最近记录: |