Notice it will convert your float type to object

df.DollarAmount.apply(lambda x : "{:,}".format(x))
Out[509]: 
0    5,721.48
1     4,000.0
2     4,769.0
3      824.07
4       643.6
5       620.0
Name: DollarAmount, dtype: object
Answer from BENY on Stack Overflow
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ dev โ€บ reference โ€บ api โ€บ pandas.io.formats.style.Styler.format.html
pandas.io.formats.style.Styler.format โ€” pandas documentation
Styler.format(formatter=None, subset=None, na_rep=None, precision=None, decimal='.', thousands=None, escape=None, hyperlinks=None)[source]#
Discussions

Thousands separator for to_csv
Pandas exposes a thousands optional parameter to read_csv used to specify a custom thousands separator, so that 1,000 or 1_000 can be successfully parsed to a numeral in the resulting DataFrame. Un... More on github.com
๐ŸŒ github.com
8
December 4, 2019
python - How to add thousand separator to numbers in pandas - Stack Overflow
Assuming that I have a pandas dataframe and I want to add thousand separators to all the numbers (integer and float), what is an easy and quick way to do it? More on stackoverflow.com
๐ŸŒ stackoverflow.com
May 25, 2020
Support parsing thousands separators in floating point data
xref #584 It seems that the decimal format works ok for the decimal sign or for the thousands but not combined. Reopen the issue? Example import pandas as pd from StringIO import StringIO data = """A;B;C 0;0,11;0,11 1.000;1000,11;1.000,1... More on github.com
๐ŸŒ github.com
3
December 24, 2012
Thousands Separator in a Number Column of Data Editor
Summary How to show thousands separator in a Number Column inside the Data Editor component introduced in streamlit 1.23? I tried to set the number format to โ€œ%,.2fโ€ as it is recognized by the defautl pandas DataFrame Styler, but this format results in erro when used in the Data Editor. More on discuss.streamlit.io
๐ŸŒ discuss.streamlit.io
0
0
June 14, 2023
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ pandas df: how to add thousand separators to a column?
r/learnpython on Reddit: Pandas df: How to add thousand separators to a column?
May 29, 2021 -

Two points here. I've 'pd.read_csv'ed a CSV file which has three columns.

I've used the following in order to extract the data and add headings to the columns (as currently the data is just naked)

Column 1 & 3 are text, and column 2 is a number.

How can I output the number with separators? EG 1,000,000 rather than 1000000

Also, what's the best way for formatting this dataframe to be included in an email body?

๐ŸŒ
GitHub
github.com โ€บ pandas-dev โ€บ pandas โ€บ issues โ€บ 30045
Thousands separator for to_csv ยท Issue #30045 ยท pandas-dev/pandas
December 4, 2019 - Pandas exposes a thousands optional parameter to read_csv used to specify a custom thousands separator, so that 1,000 or 1_000 can be successfully parsed to a numeral in the resulting DataFrame. Un...
Author ย  ghisvail
๐ŸŒ
GitHub
github.com โ€บ pandas-dev โ€บ pandas โ€บ issues โ€บ 2594
Support parsing thousands separators in floating point data ยท Issue #2594 ยท pandas-dev/pandas
December 24, 2012 - It seems that the decimal format works ok for the decimal sign or for the thousands but not combined. Reopen the issue? Example import pandas as pd from StringIO import StringIO data = """A;B;C 0;0,11;0,11 1.000;1000,11;1.000,11 20.000;20000,22;20.000,22 300.000;300000,33;300.000,33 4.000.000;4000000,44;4.000.000,44 5.000.000.000;5000000000,55;5.000.000.000,55""" df = pd.read_csv(StringIO(data), sep=';', thousands='.', decimal =',') print df.dtypes print df Results in A int64 B float64 C object A B C 0 0 1.100000e-01 0,11 1 1000 1.000110e+03 1.000,11 2 20000 2.000022e+04 20.000,22 3 300000 3.000003e+05 300.000,33 4 4000000 4.000000e+06 4.000.000,44 5 5000000000 5.000000e+09 5.000.000.000,55
Author ย  wesm
๐ŸŒ
Streamlit
discuss.streamlit.io โ€บ using streamlit
Thousands Separator in a Number Column of Data Editor - Using Streamlit - Streamlit
June 14, 2023 - Summary How to show thousands separator in a Number Column inside the Data Editor component introduced in streamlit 1.23? I tried to set the number format to โ€œ%,.2fโ€ as it is recognized by the defautl pandas DataFrame Sโ€ฆ
Find elsewhere
๐ŸŒ
Saturn Cloud
saturncloud.io โ€บ blog โ€บ how-to-format-thousand-separator-for-integers-in-a-pandas-dataframe
How to Format Thousand Separator for Integers in a Pandas DataFrame | Saturn Cloud Blog
December 6, 2023 - We are using map() instead ยท To format thousand separators for integers in a pandas DataFrame, we can define a function that takes a number as input and returns a string representation of the number with thousand separators.
๐ŸŒ
IncludeHelp
includehelp.com โ€บ python โ€บ format-a-number-with-commas-to-separate-thousands-in-pandas.aspx
Python - Format a number with commas to separate thousands in pandas
DataFrames consist of rows, columns, and data. Suppose, we have a large DataFrame with a column named X. This column has a field of large numbers (in thousands or lakhs). We need to format these numbers by putting commas in between the digits for proper data analysis.
๐ŸŒ
Mark Needham
markhneedham.com โ€บ blog โ€บ 2021 โ€บ 04 โ€บ 11 โ€บ pandas-format-dataframe-numbers-commas-decimals
Pandas - Format DataFrame numbers with commas and control decimal places | Mark Needham
April 11, 2021 - Instead of passing a single style to style.format, we can instead pass a dictionary of {"column: "style"}. So to style Population with a comma as thousands separator and PercentageVaccinated with two decimal places, we can do the following:
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ [pandas] how to specify thousand separator for int column?
r/learnpython on Reddit: [Pandas] How to specify thousand separator for int column?
August 31, 2018 -

/edit seems I found some outdated information online, read_excel() DOES support setting a thousands separator

dfa = pd.read_excel("file.xlsx", thousands=".")

Case closed

I have a column I read from Excel via read_excel("file.xlsx") with:

2.699 
2.507 
2.716 
3.229

Since I'm in the EU this are integers with values "2699, 2507, 2716, 3229" and the '.' is the thousand separator, not ','.

Panda uses the US standard so while converting it to int (there are trailing spaces so Pandas reads it as strings, then I strip them, then I convert to int) I get "2, 2, 2, 3".

read_csv() has the option to set the separator but read_excel() does not.

How can I change the thousand / comma separator to get the correct result?

Top answer
1 of 4
15

pandas (as of 0.20.1) does not allow overriding the default integer format in an easy way. It is hard coded in pandas.io.formats.format.IntArrayFormatter (the lambda function):

class IntArrayFormatter(GenericArrayFormatter):

    def _format_strings(self):
        formatter = self.formatter or (lambda x: '% d' % x)
        fmt_values = [formatter(x) for x in self.values]
        return fmt_values

I'm assuming what you're actually asking for is how you can override the format for all integers: modify (i.e. "monkey patch") the IntArrayFormatter to print integer values with thousands separated by comma as follows:

import pandas

class _IntArrayFormatter(pandas.io.formats.format.GenericArrayFormatter):

    def _format_strings(self):
        formatter = self.formatter or (lambda x: ' {:,}'.format(x))
        fmt_values = [formatter(x) for x in self.values]
        return fmt_values

pandas.io.formats.format.IntArrayFormatter = _IntArrayFormatter

Note:

  • before 0.20.0, the formatters were in pandas.formats.format.
  • before 0.18.1, the formatters were in pandas.core.format.

Aside

For floats you do not need to jump through those hoops since there is a configuration option for it:

display.float_format: The callable should accept a floating point number and return a string with the desired format of the number. This is used in some places like SeriesFormatter. See core.format.EngFormatter for an example.

2 of 4
8

The formatters parameter in to_html will take a dictionary of column names mapped to a formatting function. Below has an example of a function to build a dict that maps the same function to both floats and ints.

In [250]: num_format = lambda x: '{:,}'.format(x)

In [246]: def build_formatters(df, format):
     ...:     return {column:format 
     ...:               for (column, dtype) in df.dtypes.iteritems()
     ...:               if dtype in [np.dtype('int64'), np.dtype('float64')]}
     ...: 

In [247]: formatters = build_formatters(df_int, num_format)


In [249]: print df_int.to_html(formatters=formatters)
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>A</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>20,000</td>
    </tr>
    <tr>
      <th>1</th>
      <td>10,000</td>
    </tr>
  </tbody>
</table>
๐ŸŒ
Medium
medium.com โ€บ data-science โ€บ apply-thousand-separator-and-other-formatting-to-pandas-dataframe-45f2f4c7ab01
Apply Thousand Separator (and Other Formatting) to Pandas Dataframe | by Analyst Sharone | TDS Archive | Medium
January 18, 2023 - Apply Thousand Separator (and Other Formatting) to Pandas Dataframe Save Yourself Some Google Search Time by Learning These Useful Pandas Formatting Tricks Introduction Formatting data in a pandas โ€ฆ
๐ŸŒ
Streamlit
discuss.streamlit.io โ€บ using streamlit
Number with a space for thousands separator to be aligned to the right - Using Streamlit - Streamlit
October 11, 2022 - In a dataframe i want to right align a numbers with a space for thousands separator. The problem is that when numbers are with space for thousands separator, they become a string and automatically aligned to the left.
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-2255.html
thousands separator format on df column
March 2, 2017 - Dear Python Experts, I an trying to convert my PopEst column so a thousands separator gets added. def thirteen(): Top15 = answer_one() Top15['PopEst'] = Top15['Energy Supply'] / Top15['Energy Supply per Capita'] PopEst = To...
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ reference โ€บ api โ€บ pandas.io.formats.style.Styler.format.html
pandas.io.formats.style.Styler.format โ€” pandas 3.0.1 documentation
Styler.format(formatter=None, subset=None, na_rep=None, precision=None, decimal='.', thousands=None, escape=None, hyperlinks=None)[source]#