python - 将 DataFrame 的负号值转换为括号
问题描述
我已经使用 - pd.options.display.float_format='{:,.0f}'.format 设置了 DF 数字格式
但是,这会导致显示负值,例如-100,000。我希望该值显示为 (100,000)。帮助表示赞赏
解决方案
尝试:
df = pd.DataFrame({"a":[1, -23]})
print(df)
a
0 1
1 -23
df.astype(str).a.str.replace("^-([0-9]*)", "(\\1)")
0 1
1 (23)
Name: a, dtype: object
注意:您所要求的仅用于演示目的。
对于小数,您可以尝试:
df.a.astype(str).str.replace("^-([0-9.]*)", "(\\1)")
0 1.0
1 (23.0)
Name: a, dtype: object
或者,如果您愿意:
df.a.astype(str).str.replace("^-([0-9]+\.[0-9]+)", "(\\1)")
0 1.0
1 (23.0)
Name: a, dtype: object
推荐阅读
- php - php mysql 查询结果 utf8mb4_bin 编码为 utf-8 for html
- javascript - 二次表达式。错误
- python - 如何使用 Tensorflow keras 标记化
- google-vault-api - 关于保管库的审核日志
- awk - 与列表项关联的查找值
- r - 使用列表命名对象
- powershell - 尝试捕获块后Powershell代码未退出
- compiler-construction - 如何根据这个转换图识别数字?
- flutter - 如何更改列表中的单个按钮颜色 - 颤动
- android - 如何在键盘视图中显示自定义视图,如 imageview 或 gif 视图