pandas DataFrame 样式丢失 table 边框

pandas DataFrame style lost table border

我正在按照 https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html 的说明进行操作 为我的数据框设置样式成为 html.

效果很好,除了每个 row/column 的 table 边框丢失了。现在是无边框的table.

如何添加边框?

pandas这种风格我平时不怎么用,所以查了一下。以下代码将为您提供帮助。这就像我们有风格的优先权。 您稍后设置的优先。貌似可以一次指定选择器,但是需要每一个都指定。

import numpy as np
import pandas as pd
from IPython.display import display, HTML

df = pd.DataFrame([[1, 2], [3, 4]], index=["a", "b"], columns=["A", "B"])

style = df.style.set_table_styles(
    [{"selector": "", "props": [("border", "1px solid grey")]},
      {"selector": "tbody td", "props": [("border", "1px solid grey")]},
     {"selector": "th", "props": [("border", "1px solid grey")]}
    ]
)


HTML(style.render())