用逗号格式化数字以在 Python 中分隔千位

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43102734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:31:22  来源:igfitidea点击:

Format a number with commas to separate thousands in Python

pythonpython-3.xpandasjupyter-notebooknumber-formatting

提问by Sandy Tumma

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.

我有一个大型数据框,其中有一列名为Lead Rev. 此列是一个数字字段,例如(100000 或 5000 等)我想知道如何格式化这些数字以将逗号显示为千位分隔符。该数据集有超过 200,000 行。

Is it something like: '{:,}'.format('Lead Rev')

是不是像这样: '{:,}'.format('Lead Rev')

which gives this error:

这给出了这个错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-182-5fe9c827d80b> in <module>()
----> 1 '{:,}'.format('Lead Rev')

ValueError: Cannot specify ',' or '_' with 's'.

回答by jeffhale

To make all your floats show comma separators by default in pandas versions 0.23 through 0.25 set the following:

要使所有浮点数在 0.23 到 0.25 版本的 Pandas 中默认显示逗号分隔符,请设置以下内容:

pd.options.display.float_format = '{:,}'.format

https://pandas.pydata.org/pandas-docs/version/0.23.4/options.html

https://pandas.pydata.org/pandas-docs/version/0.23.4/options.html

In pandas version 1.0 this leads to some strange formatting in some cases.

在 pandas 1.0 版中,这在某些情况下会导致一些奇怪的格式。

回答by flivan

You can use apply() to get the desired result. This works with floating too

您可以使用 apply() 来获得所需的结果。这也适用于浮动

import pandas as pd

series1 = pd.Series({'Value': 353254})
series2 = pd.Series({'Value': 54464.43})
series3 = pd.Series({'Value': 6381763761})

df = pd.DataFrame([series1, series2, series3])
print(df.head())

         Value
0  3.532540e+05
1  5.446443e+04
2  6.381764e+09

df['Value'] = df.apply(lambda x: "{:,}".format(x['Value']), axis=1)
print(df.head())

             Value
0        353,254.0
1        54,464.43
2  6,381,763,761.0

回答by Ran Feldesh

df.head().style.format("{:,.0f}")(for all columns)

df.head().style.format("{:,.0f}")对于所有列

df.head().style.format({"col1": "{:,.0f}", "col2": "{:,.0f}"})(per column)

df.head().style.format({"col1": "{:,.0f}", "col2": "{:,.0f}"})每列

https://pbpython.com/styling-pandas.html

https://pbpython.com/styling-pandas.html

回答by user2357112 supports Monica

Grouping options like ,are only supported for numeric presentation types. You need to specify a numeric presentation type. Read up on your options.

,仅数字表示类型支持类似的分组选项。您需要指定数字表示类型。仔细阅读您的选择

回答by Sassaba

You can use apply or stack method

您可以使用 apply 或 stack 方法

df.apply(lambda x: x.str.replace(',','.'))
df.stack().str.replace(',','.').unstack()