[英]Use pandas style using comma as decimal separator
我想使用熊猫风格,并使用逗号作为小数点分隔符。
以这个数据框为例:
tb = pd.DataFrame({"Variable": [5, 4.5, 4.8, 4.7], "std_err": [1, .06, .09, .17]},
index = ['Group A', 'Group B', 'Group C', 'Group D'])
多变的 | 标准错误 | |
---|---|---|
A组 | 5.0 | 1.00 |
B组 | 4.5 | 0.06 |
C组 | 4.8 | 0.09 |
D组 | 4.7 | 0.17 |
我试过这个:
locale.setlocale(locale.LC_ALL, 'pt_BR.UTF-8')
pd.set_option("float_format", locale.str)
tb = pd.DataFrame({"Variable": [4.9853, 4.5345, 4.8542, 4.7234],
"std_err": [.9234, .0676, .0917, .1784]},
index = ['Group A', 'Group B', 'Group C', 'Group D'])
tb.style.bar()
但我希望看到逗号作为小数点分隔符和样式栏,而是我得到了一个用样式格式化的表格,但忽略了语言环境。
我可以两者兼得吗? 用样式(条形、颜色等)格式化并用逗号作为小数点分隔符的表格?
最直接的解决方法是指定Styler.format
的decimal
参数:
tb.style.format(decimal=',').bar()
其他替代方法包括将formatter
设置为locale.str
类的locale.str
locale.setlocale(locale.LC_ALL, 'pt_BR.UTF-8')
tb.style.format(formatter=locale.str).bar()
locale.setlocale(locale.LC_ALL, 'pt_BR.UTF-8')
tb.style.format(
formatter=lambda x: locale.format_string('%.6f', x)
).bar()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.