使用 pandas 样式,使用逗号作为小数点分隔符
Use pandas style using comma as decimal separator
我想使用 pandas 样式,并使用逗号作为小数点分隔符。
以这个dataframe为例:
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'])
Variable
std_err
Group A
5.0
1.00
Group B
4.5
0.06
Group C
4.8
0.09
Group 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()
但我希望看到逗号作为小数点分隔符和样式栏,而我得到的是 table 格式的样式,但忽略了区域设置。
我可以两者兼得吗? table 格式化为样式(条形、颜色等)并以逗号作为小数点分隔符?
最直接的修复方法是指定 Styler.format
的 decimal
参数:
tb.style.format(decimal=',').bar()
其他替代方法包括将 formatter
设置为类似 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()
我想使用 pandas 样式,并使用逗号作为小数点分隔符。
以这个dataframe为例:
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'])
Variable | std_err | |
---|---|---|
Group A | 5.0 | 1.00 |
Group B | 4.5 | 0.06 |
Group C | 4.8 | 0.09 |
Group 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()
但我希望看到逗号作为小数点分隔符和样式栏,而我得到的是 table 格式的样式,但忽略了区域设置。
我可以两者兼得吗? table 格式化为样式(条形、颜色等)并以逗号作为小数点分隔符?
最直接的修复方法是指定 Styler.format
的 decimal
参数:
tb.style.format(decimal=',').bar()
其他替代方法包括将 formatter
设置为类似 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()