用逗号格式化数字以在 Python 中分隔千位 [英] Format a number with commas to separate thousands in Python
问题描述
我有一个大型数据框,其中有一列名为 Lead Rev
.此列是一个数字字段,例如(100000 或 5000 等)我想知道如何格式化这些数字以将逗号显示为千位分隔符.数据集有超过 200,000 行.
I have a large dataframe which has a column called Lead Rev
. This column is a field of numbers such as (100000 or 5000 etc.) I want to know how to format these numbers to show commas as thousand separators. The dataset has over 200,000 rows.
是不是类似:'{:,}'.format('Lead Rev')
这给出了这个错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-182-5fe9c827d80b> in <module>()
----> 1 '{:,}'.format('Lead Rev')
ValueError: Cannot specify ',' or '_' with 's'.
推荐答案
要使您的所有浮点数在 0.23 到 0.25 版本的 Pandas 中默认显示逗号分隔符,请设置以下内容:
To make all your floats show comma separators by default in pandas versions 0.23 through 0.25 set the following:
pd.options.display.float_format = '{:,}'.format
https://pandas.pydata.org/pandas-docs/version/0.23.4/options.html
在 pandas 1.0 版中,这在某些情况下会导致一些奇怪的格式.
In pandas version 1.0 this leads to some strange formatting in some cases.
这篇关于用逗号格式化数字以在 Python 中分隔千位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!