You could monkey-patch pandas.io.formats.format.IntArrayFormatter:

import contextlib
import numpy as np
import pandas as pd
import pandas.io.formats.format as pf
np.random.seed(2015)

@contextlib.contextmanager
def custom_formatting():
    orig_float_format = pd.options.display.float_format
    orig_int_format = pf.IntArrayFormatter

    pd.options.display.float_format = '{:0,.2f}'.format
    class IntArrayFormatter(pf.GenericArrayFormatter):
        def _format_strings(self):
            formatter = self.formatter or '{:,d}'.format
            fmt_values = [formatter(x) for x in self.values]
            return fmt_values
    pf.IntArrayFormatter = IntArrayFormatter
    yield
    pd.options.display.float_format = orig_float_format
    pf.IntArrayFormatter = orig_int_format


df = pd.DataFrame(np.random.randint(10000, size=(5,3)), columns=list('ABC'))
df['D'] = np.random.random(df.shape[0])*10000

with custom_formatting():
    print(df)

yields

      A     B     C        D
0 2,658 2,828 4,540 8,961.77
1 9,506 2,734 9,805 2,221.86
2 3,765 4,152 4,583 2,011.82
3 5,244 5,395 7,485 8,656.08
4 9,107 6,033 5,998 2,942.53

while outside of the with-statement:

print(df)

yields

      A     B     C            D
0  2658  2828  4540  8961.765260
1  9506  2734  9805  2221.864779
2  3765  4152  4583  2011.823701
3  5244  5395  7485  8656.075610
4  9107  6033  5998  2942.530551
Answer from unutbu on Stack Overflow
Top answer
1 of 4
23

You could monkey-patch pandas.io.formats.format.IntArrayFormatter:

import contextlib
import numpy as np
import pandas as pd
import pandas.io.formats.format as pf
np.random.seed(2015)

@contextlib.contextmanager
def custom_formatting():
    orig_float_format = pd.options.display.float_format
    orig_int_format = pf.IntArrayFormatter

    pd.options.display.float_format = '{:0,.2f}'.format
    class IntArrayFormatter(pf.GenericArrayFormatter):
        def _format_strings(self):
            formatter = self.formatter or '{:,d}'.format
            fmt_values = [formatter(x) for x in self.values]
            return fmt_values
    pf.IntArrayFormatter = IntArrayFormatter
    yield
    pd.options.display.float_format = orig_float_format
    pf.IntArrayFormatter = orig_int_format


df = pd.DataFrame(np.random.randint(10000, size=(5,3)), columns=list('ABC'))
df['D'] = np.random.random(df.shape[0])*10000

with custom_formatting():
    print(df)

yields

      A     B     C        D
0 2,658 2,828 4,540 8,961.77
1 9,506 2,734 9,805 2,221.86
2 3,765 4,152 4,583 2,011.82
3 5,244 5,395 7,485 8,656.08
4 9,107 6,033 5,998 2,942.53

while outside of the with-statement:

print(df)

yields

      A     B     C            D
0  2658  2828  4540  8961.765260
1  9506  2734  9805  2221.864779
2  3765  4152  4583  2011.823701
3  5244  5395  7485  8656.075610
4  9107  6033  5998  2942.530551
2 of 4
12

Another option for Jupyter notebooks is to use df.style.format('{:,}'), but it only works on a single dataframe as far as I know, so you would have to call this every time:

table.style.format('{:,}')
          col1       col2
0s   9,246,452  6,669,310
>0   2,513,002  5,090,144

table
       col1     col2
0s  9246452  6669310
>0  2513002  5090144

Styling — pandas 1.1.2 documentation

🌐
Pandas
pandas.pydata.org › docs › user_guide › options.html
Options and settings — pandas 3.0.1 documentation - PyData |
Ignored when `max_rows` is set to None or 0. When set to None, follows the value of `max_rows`. [default: 10] [currently: 10] display.multi_sparse : boolean "sparsify" MultiIndex display (don't display repeated elements in outer levels within groups) [default: True] [currently: True] display.notebook_repr_html : boolean When True, IPython notebook will use html representation for pandas objects (if it is available). [default: True] [currently: True] display.pprint_nest_depth : int Controls the number of nested levels to process when pretty-printing [default: 3] [currently: 3] display.precision : int Floating point output precision in terms of number of places after the decimal, for regular formatting as well as scientific notation.
Discussions

ENH: Expose integer formatting also for default display, not only styler
I noticed that in Pandas version 2.2 the internal implementation of IntArrayFormatter was changed (or at least renamed), marking the classes as private using one underscore. I used the implementation as described here to format integers in a DataFrame on each display. More on github.com
🌐 github.com
14
January 31, 2024
python - How to display pandas DataFrame of floats using a format string for columns? - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I would like to display a pandas dataframe with a given format using print() and the IPython display(). More on stackoverflow.com
🌐 stackoverflow.com
How to terminate or remove the .0 point from an int.
Try using the g flag when you print it. x = 5.0 y = 5.1 print("regular print:", x, y) print("with g flag:", f"{x:g} {y:g}") More on reddit.com
🌐 r/learnpython
5
2
December 9, 2022
Pandas df: How to add thousand separators to a column?
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. More on reddit.com
🌐 r/learnpython
7
1
May 30, 2021
🌐
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
If a callable then that function should take a data value as input and return a displayable representation, such as a string. If formatter is given as a string this is assumed to be a valid Python format specification and is wrapped to a callable as string.format(x). If a dict is given, keys should correspond to column names, and values should be string or callable, as above. The default formatter currently expresses floats and complex numbers with the pandas display precision unless using the precision argument here.
🌐
CSDN
devpress.csdn.net › python › 630453b47e66823466199e31.html
Can you format pandas integers for display, like `pd.options.display.float_format` for floats?_python_Mangs-Python
You could monkey-patch pandas.io.formats.format.IntArrayFormatter: import contextlib import numpy as np import pandas as pd import pandas.io.formats.format as pf np.random.seed(2015) @contextlib.contextmanager def custom_formatting(): orig_float_format = pd.options.display.float_format orig_int_format = pf.IntArrayFormatter pd.options.display.float_format = '{:0,.2f}'.format class IntArrayFormatter(pf.GenericArrayFormatter): def _format_strings(self): formatter = self.formatter or '{:,d}'.format fmt_values = [formatter(x) for x in self.values] return fmt_values pf.IntArrayFormatter = IntArrayFormatter yield pd.options.display.float_format = orig_float_format pf.IntArrayFormatter = orig_int_format df = pd.DataFrame(np.random.randint(10000, size=(5,3)), columns=list('ABC')) df['D'] = np.random.random(df.shape[0])*10000 with custom_formatting(): print(df)
🌐
GeeksforGeeks
geeksforgeeks.org › formatting-integer-column-of-dataframe-in-pandas
Formatting float column of Dataframe in Pandas - GeeksforGeeks
January 10, 2024 - While presenting the data, showing the data in the required format is also a crucial part. Sometimes, the value is so big that we want to show only the desired part of this or we can say in some desired format. Let's see different methods of formatting integer columns and the data frame it in Pandas
🌐
GitHub
github.com › pandas-dev › pandas › issues › 6502
int_format in display options · Issue #6502 · pandas-dev/pandas
February 27, 2014 - pd.options.display.formatter= {'int': formatter, 'float': formatter} or both. mroeschke added the Enhancement label · May 16, 2020 · mroeschke removed the IO CSV · read_csv, to_csv label · Apr 11, 2021 · mroeschke removed this from the Someday milestone · Oct 13, 2022 · Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment · Labels · Enhancement Output-Formatting · __repr__ of pandas objects, to_string ·
🌐
GitHub
github.com › pandas-dev › pandas › issues › 57177
ENH: Expose integer formatting also for default display, not only styler · Issue #57177 · pandas-dev/pandas
January 31, 2024 - import pandas as pd pd.options.styler.format.thousands = ',' s = pd.Series([1_000_000]).to_frame() s.style # does change display of s to 1,000,000 ... if pd.options.format.thousands: format_str = f':{pd.options.format.thousands}d'.format else: format_str = ':d'.format ... class IntArrayFormatter(pf._GenericArrayFormatter): def _format_strings(self): formatter = self.formatter or '{:,d}'.format fmt_values = [formatter(x) for x in self.values] return fmt_values pf._IntArrayFormatter = IntArrayFormatter
Author   enryH
Find elsewhere
🌐
CodeProject
codeproject.com › Questions › 5297059 › Pd-options-display-float-format-for-integer
Pd.options.display.float_format for integer - CodeProject
March 13, 2021 - Free source code and tutorials for Software developers and Architects.; Updated: 13 Mar 2021
🌐
Pandas
pandas.pydata.org › pandas-docs › stable › user_guide › options.html
Options and settings — pandas 3.0.1 documentation
pandas also allows you to set how numbers are displayed in the console. This option is not set through the set_options API. Use the set_eng_float_format function to alter the floating-point formatting of pandas objects to produce a particular format.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.io.formats.style.Styler.format_index.html
pandas.io.formats.style.Styler.format_index — pandas 3.0.1 documentation
If a callable then that function should take a label value as input and return a displayable representation, such as a string. If formatter is given as a string this is assumed to be a valid Python format specification and is wrapped to a callable as string.format(x). If a dict is given, keys should correspond to MultiIndex level numbers or names, and values should be string or callable, as above. The default formatter currently expresses floats and complex numbers with the pandas display precision unless using the precision argument here.
🌐
Stacked Turtles
kiwidamien.github.io › stylish-pandas.html
Stylish Pandas
November 6, 2019 - To set the number format for all dataframes, use pd.options.display.float_format to a function.
🌐
Saturn Cloud
saturncloud.io › blog › can-you-format-pandas-integers-for-display
Can You Format Pandas Integers for Display | Saturn Cloud Blog
August 25, 2023 - Unfortunately, pandas does not provide a built-in function to format integers for display in the same way as pd.options.display.float_format for floats. By default, pandas displays integers as plain integers with no formatting.
🌐
Pandas
pandas.pydata.org › pandas-docs › stable › user_guide › options.html
Options and settings — pandas 2.2.2 documentation - PyData |
February 12, 2022 - Ignored when `max_rows` is set to None or 0. When set to None, follows the value of `max_rows`. [default: 10] [currently: 10] display.multi_sparse : boolean "sparsify" MultiIndex display (don't display repeated elements in outer levels within groups) [default: True] [currently: True] display.notebook_repr_html : boolean When True, IPython notebook will use html representation for pandas objects (if it is available). [default: True] [currently: True] display.pprint_nest_depth : int Controls the number of nested levels to process when pretty-printing [default: 3] [currently: 3] display.precision : int Floating point output precision in terms of number of places after the decimal, for regular formatting as well as scientific notation.
🌐
Linux find Examples
queirozf.com › entries › pandas-display-options-examples-and-reference
Pandas Display Options: Examples and Reference
August 22, 2023 - import pandas as pd pd.set_option('display.float_format', lambda x: '%8.2f' % x) pd.DataFrame({ "num":[0.0001, 10000000,100,0.5], "str":["foo","bar","baz","quux"] }) BEFORE: default options · AFTER setting display.float_format · To use commas (',') as thousands separator for floats and integers: Use pandas.set_option('display.float_format', lambda x: '{:,.0f}' % x) to use comma separators and no decimal places ·
🌐
Krbnite
krbnite.github.io › HTML-Reports-Those-Numbers-Need-Commas
HTML Reports: Those Numbers Need Commas!
January 17, 2018 - As for the integer variable: pandas doesn’t necessarily care that it’s an integer, but that it’s not a pandas object. Even float(num) would not benefit from setting pandas options. [In] pd.options.display.float_format = '{:,}'.format [In] [In] num = 123456789 [In] df = pd.DataFrame({'num': [123456789.]}) [In] df [Out] num 0 123,456,789.0 [In] [In] num [Out] 123456789 [In] float(num) [Out] 123456789.0 ·
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 1.5 › reference › api › pandas.io.formats.style.Styler.format.html
pandas.io.formats.style.Styler.format — pandas 1.5.2 documentation
Character used as thousands separator for floats, complex and integers. New in version 1.3.0. ... Use ‘html’ to replace the characters &, <, >, ', and " in cell display string with HTML-safe sequences. Use ‘latex’ to replace the characters &, %, $, #, _, {, }, ~, ^, and \ in the cell display string with LaTeX-safe sequences. Escaping is done before formatter.