用逗号格式化数字以在Python中分隔成千上万 [英] Format a number with commas to separate thousands in Python

查看:446
本文介绍了用逗号格式化数字以在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版本的熊猫中,让所有的float都显示逗号分隔符,请设置以下内容:

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

在熊猫1.0版中,这在某些情况下会导致某些奇怪的格式.

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

这篇关于用逗号格式化数字以在Python中分隔成千上万的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆