python - Pandas:语言环境格式在 style.format() 中不起作用
问题描述
我想pandas.Dataframe
用 locale渲染一个"de_DE.UTF-8"
,有一个“,”作为小数点和“。” 作为千位分隔符。简单地运行时locale.format
,我会得到预期的结果。但是,当添加与 pandas 格式化程序相同的表达式时,呈现的 html 中没有任何变化(尽管没有引发错误)。示例代码:
import pandas
import locale
locale.setlocale(locale.LC_NUMERIC, 'de_DE.UTF-8')
print(locale.format('%.2f', 1000.245, True))
print(locale.format('%.2f', 10000000.22655, True))
df = pandas.DataFrame({'a': [1000.245, 10000000.22655]})
style = df.style.format(formatter=lambda x: f'{locale.format("%.2f", x, True)} €')
print(style.render())
给出输出:
1.000,25
10.000.000,23
<style type="text/css" >
</style>
<table id="T_f0ae1678_3e71_11e9_8f47_d0bf9ce00d56" >
<thead> <tr>
<th class="blank level0" ></th>
<th class="col_heading level0 col0" >a</th>
</tr></thead>
<tbody> <tr>
<th id="T_f0ae1678_3e71_11e9_8f47_d0bf9ce00d56level0_row0" class="row_heading level0 row0" >0</th>
<td id="T_f0ae1678_3e71_11e9_8f47_d0bf9ce00d56row0_col0" class="data row0 col0" >1000.25 €</td>
</tr> <tr>
<th id="T_f0ae1678_3e71_11e9_8f47_d0bf9ce00d56level0_row1" class="row_heading level0 row1" >1</th>
<td id="T_f0ae1678_3e71_11e9_8f47_d0bf9ce00d56row1_col0" class="data row1 col0" >10000000.23 €</td>
</tr></tbody>
</table>
pandas 有自己的设置locale
吗?或者我错过了什么?谢谢
解决方案
这似乎是旧熊猫版本(<2020)中的一个错误,请更新您的熊猫。
版本:“1.1.3”输出:
<style type="text/css" >
</style><table id="T_82df8768_b9b1_11eb_a80f_88d7f6c760ad" ><thead> <tr> <th class="blank level0" ></th> <th class="col_heading level0 col0" >a</th> </tr></thead><tbody>
<tr>
<th id="T_82df8768_b9b1_11eb_a80f_88d7f6c760adlevel0_row0" class="row_heading level0 row0" >0</th>
<td id="T_82df8768_b9b1_11eb_a80f_88d7f6c760adrow0_col0" class="data row0 col0" >1.000,25 €</td>
</tr>
<tr>
<th id="T_82df8768_b9b1_11eb_a80f_88d7f6c760adlevel0_row1" class="row_heading level0 row1" >1</th>
<td id="T_82df8768_b9b1_11eb_a80f_88d7f6c760adrow1_col0" class="data row1 col0" >10.000.000,23 €</td>
</tr>
</tbody></table>
推荐阅读
- arrays - 使用 Postman 为对象数组发送 GET 请求
- c++ - 系统地缩放 C++ 函数的返回结构
- ckeditor - 无界的 CkEditor5 视图值
- python - 如何使用“onehot_enc”实现朴素贝叶斯算法?
- symfony - Nginx symfony““try_files”指令中的参数数量无效”
- javascript - 当存在具有特定名称的键时,将对象推入数组中
- android - 添加依赖项 CardView 和 RecylerView 时出现 Gradle 错误
- java - Java 转换器空元素
- angular6 - Splice 返回已删除的元素,而不是返回所有其他未删除的元素
- ibm-cloud - IBM Watson Assistant:聊天机器人实体区别