admin管理员组文章数量:1122832
How to add space between different groups when printing a pandas dataframe? The purpose is to separate different groups for improved visualization. Ideally both for column groups and row groups, also even for multi index groups.
Here I provide an image, on which arrows show an example spacing.
import pandas as pd
# Example DataFrame with MultiIndex
index = pd.MultiIndex.from_tuples([
('Group 1', 'Row 1'),
('Group 1', 'Row 2'),
('Group 2', 'Row 1')
], names=['Group', 'Subgroup'])
columns = pd.MultiIndex.from_tuples([
('Metric A', 'Sub A1'),
('Metric A', 'Sub A2'),
('Metric B', 'Sub B1')
], names=['Metric', 'Submetric'])
data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
df = pd.DataFrame(data, index=index, columns=columns)
print(df)
The output is:
Metric Metric A Metric B
Submetric Sub A1 Sub A2 Sub B1
Group Subgroup
Group 1 Row 1 1 2 3
Row 2 4 5 6
Group 2 Row 1 7 8 9
We want to be able to add spacing like this for an improved look:
Metric Metric A | Metric B
Submetric Sub A1 Sub A2 | Sub B1
Group Subgroup |
Group 1 Row 1 1 2 | 3
Row 2 4 5 | 6
---------------- ----------------+---------
Group 2 Row 1 7 8 | 9
We added spacing between groups, both on grouped columns and grouped indices.
I checked pandas styling documentation but it seems like this case isn't considered.
本文标签: pythonpandas dataframe styling to add space between different groupsStack Overflow
版权声明:本文标题:python - pandas dataframe styling to add space between different groups - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736283817a1927090.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论