tabulate:好看的字符串表格库
source link: https://www.tuicool.com/articles/FNVFFj2
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
写公众号文章dataframe运行的结果直接复制粘贴到公众号格式会乱,今天看到一个tabulate库可以解决这个问题。
tabulate参数
tabulate(data, headers, showindex, tablefmt, colalign, numalign, stralign, floatfmt)
-
data 传入的数据
-
headers 字段名,参数可以是firstrow,也可以是指定的列名列表
-
showindex 显示行索引。布尔值,或者 行索引列表
-
tablefmt 表格样式,常见的有github、plain、psql等
-
colalign、numalign、stralign 对齐方式,可以传入列表或者字符串,参数有right、center、left
-
floatfmt 浮点型对齐方式,可以传入列表或者字符串。例如 “.4f”小数点后4位
tabulate支持传入的数据为
-
列表的列表
-
字典列表(关键词作为列名)
-
二维numpy数组
-
pandas.DataFrame
-
...
安装
qucik start
这里我自定义了一个函数 println(data,**kw)
-
data 传入的数据
-
关键词参数。关键词可以是headers、showindex、tablefmt、colalign, numalign, stralign, floatfmt
这样后面我只需要调用 println(data,**kw)
即可
Run
headers
显示字段名
-
传入字段名列表
-
或者“firstrow”
Run
headers也可以传入"firstrow"
Run
showindex
显示行索引值,传入True或False
或者
行索引值列表
Run
显示行索引
Run
自定义行索引 ['第一', '第二']
Run
tablefmt
设定表格样式,支持
-
"plain"
-
"simple"
-
"github"
-
"grid"
-
"fancy_grid"
-
"pipe"
-
"orgtbl"
-
"jira"
-
"presto"
-
"psql"
-
"rst"
-
"mediawiki"
-
"moinmoin"
-
"youtrack"
-
"html"
-
"latex"
-
"latex_raw"
-
"latex_booktabs"
-
"textile"
大家都试一下tablefmt的参数,选择自己喜欢的格式
Run
字段名, 表格样式位github
Run
字段名, 表格样式位psql
Run
对齐方式
设定对齐方式的参数有
-
colalign
-
numalign
-
stralign
-
floatfmt
前三个的参数都可以用center、left、right、decimal设置。
floatfmt需要用特殊的方法, 例如 ".5f"小数点后五位
Run
左对齐
左对齐, 小数点后三位
Run
近期文章
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK