Format a number with commas to separate thousands in Python

Sandy Tumma picture Sandy Tumma · Mar 29, 2017 · Viewed 55.2k times · Source

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.

Is it something like: '{:,}'.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'.

Answer

jeffhale picture jeffhale · Sep 19, 2018

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

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