To get the separator you can do: df['col'] = df['col'].apply(lambda x : "{:,}".format(x)) Beware that this converts your integers/floats to strings. To include a Dataframe in an email body, I think converting the df to an html table would make sense. See pandas' .to_html() method. Answer from sarrysyst on reddit.com
๐ŸŒ
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?

Discussions

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
excel - How to insert a comma as a thousands separator in a pandas dataframe column? - Stack Overflow
I'm trying to format the Dollar Amount column to have a comma thousands separator for easier viewing, but I haven't been able to figure it out. Can someone please show me the way? import pandas as... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Formatting thousand separator for integers in a pandas dataframe - Stack Overflow
Communities for your favorite technologies. Explore all Collectives ยท Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
๐ŸŒ stackoverflow.com
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
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ user_guide โ€บ reshaping.html
Reshaping and pivot tables โ€” pandas 3.0.1 documentation
As with the Series version, you can pass values for the prefix and prefix_sep. By default the column name is used as the prefix and _ as the prefix separator.
๐ŸŒ
Real Python
realpython.com โ€บ how-to-pandas-pivot-table
How to Create Pivot Tables With pandas โ€“ Real Python
May 24, 2024 - For this example, you can ignore the overall total rows and columns, but you should apply a currency format using the $ symbol and use the underscore (_) character as your thousands separator.
๐ŸŒ
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:
Find elsewhere
๐ŸŒ
Pandas
pandas.pydata.org โ€บ pandas-docs โ€บ stable โ€บ user_guide โ€บ reshaping.html
Reshaping and pivot tables โ€” pandas 2.3.3 documentation
As with the Series version, you can pass values for the prefix and prefix_sep. By default the column name is used as the prefix and _ as the prefix separator.
๐ŸŒ
Appsloveworld
appsloveworld.com โ€บ pandas โ€บ 100 โ€บ 439 โ€บ how-to-format-pivot-table-numbers-with-a-thousands-seperator-and-negative-numbers
[Code]-How to format pivot table numbers with a thousands seperator and negative numbers in brackets?-pandas
How to replace a word in a column, which is present in the pandas dataframe by another word in the same column? ... R merge data frames, allow inexact ID matching (e.g. with additional characters 1234 matches ab1234 ) ... I would like to format my negative numbers in "Accounting" format, i.e. with brackets and a comma as a thousands sepertor. For example, I would like to format -1000000 as (1,000,000). I was able to format my numbers (in a pivot table) with a thousand separator ...
๐ŸŒ
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.
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>
๐ŸŒ
Python Forum
python-forum.io โ€บ Thread-thousands-separator-format-on-df-column
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 โ€บ pandas-docs โ€บ version โ€บ 1.4 โ€บ user_guide โ€บ reshaping.html
Reshaping and pivot tables โ€” pandas 1.4.4 documentation
While pivot() provides general purpose pivoting with various data types (strings, numerics, etc.), pandas also provides pivot_table() for pivoting with aggregation of numeric data.
๐ŸŒ
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]#
๐ŸŒ
Pandas
pandas.pydata.org โ€บ pandas-docs โ€บ version โ€บ 1.5 โ€บ user_guide โ€บ reshaping.html
Reshaping and pivot tables โ€” pandas 1.5.2 documentation
While pivot() provides general purpose pivoting with various data types (strings, numerics, etc.), pandas also provides pivot_table() for pivoting with aggregation of numeric data.
๐ŸŒ
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โ€ฆ
๐ŸŒ
Pythonfixing
pythonfixing.com โ€บ 2022 โ€บ 01 โ€บ fixed-pandas-seriesdataframe-format.html
[FIXED] Pandas Series/Dataframe: Format string numbers with commas as Thousands Separators ~ PythonFixing
January 23, 2022 - Is there a way to format numbers stored as string so that they have commas as thousand separator? At the end the series has to be of type object, as I need to be able to search the series for "," using df.str.contains() ... import pandas as pd import numpy as np def format_float(x): try: flt = float(x) return "{:,}".format(flt) except: return x s = {'A': "Hello", 'B': "593753", 'C': "16.8|", "D" : np.nan, "E":"%"} s = pd.Series(data=s, index=['A', 'B', 'C',"D","E"]) s2 = s.apply(lambda x: format_float(x)) Answered By - Pankaj Saini