小编Lut*_*utz的帖子

Pandas 风格:在整行上绘制边框,包括多索引

我在 jupyter 笔记本中使用 pandas 样式来强调此数据框中子组之间的边界:

(从技术上讲:在每个更改的多重索引处绘制边框,但忽略最低级别)

# some sample df with multiindex
res = np.repeat(["response1","response2","response3","response4"], 4)
mod = ["model1", "model2","model3","model4"]*len(res)
data = np.random.randint(0,50,size=len(mod))
df = pd.DataFrame(zip(res,mod,data), columns=["res","mod","data"])
df.set_index(["res","mod"], inplace=True)

# set borders at individual frequency
indices_with_borders = range(0,len(df), len(np.unique(mod)))
df.style.set_properties(subset=(df.index[indices_with_borders], df.columns), **{
                      'border-width': '1px', "border-top-style":"solid"}) 
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述

现在看起来有点傻,边框只绘制在列上,而不是一直贯穿多重索引。这将是一种更令人愉悦的风格:

在此输入图像描述

有谁知道如何/是否可以实现?提前致谢!

python pandas jupyter-notebook pandas-styles

5
推荐指数
1
解决办法
7728
查看次数

标签 统计

jupyter-notebook ×1

pandas ×1

pandas-styles ×1

python ×1