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.

Answer from jeffhale on Stack Overflow
🌐
GitHub
github.com › pandas-dev › pandas › issues › 58737
BUG: "styler.format.thousands" option doesn't work for integers · Issue #58737 · pandas-dev/pandas
May 16, 2024 - import pandas pandas.set_option('styler.format.thousands', ',') data = pandas.DataFrame(data=[{'field': 1234567890}], dtype='int64',) # Doesn't output integer field with comma separator data # Does output integer field with comma separator data.style.format(thousands=',') Printing data without .style in jupyter cell displays huge integer number without comma separator, inspite of the option set Printing data with .style in jupyter cell displays integers with commas · Outputs must be same · Details · commit : d9cdd2e python : 3.11.9.final.0 python-bits : 64 OS : Darwin OS-release : 23.4.0 Version : Darwin Kernel Version 23.4.0: Fri Mar 15 00:10:42 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6000 machine : arm64 processor : arm byteorder : little LC_ALL : None LANG : None LOCALE : None.UTF-8 ·
Published   May 16, 2024
Author   vladpy8
🌐
Pandas
pandas.pydata.org › docs › dev › reference › api › pandas.io.formats.style.Styler.format.html
pandas.io.formats.style.Styler.format — pandas documentation
When instantiating a Styler, default formatting can be applied by setting the pandas.options: styler.format.formatter: default None. styler.format.na_rep: default None. styler.format.precision: default 6. styler.format.decimal: default “.”. styler.format.thousands: default None.
🌐
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: '{:,.2f}'.format(x)) pd.DataFrame({ "num":[100000, 100,100,200.50], "str":["foo","bar","baz","quux"] }) BEFORE: default display options · AFTER: using commas as thousands separator and 2 decimal places ·
🌐
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
When instantiating a Styler, default formatting can be applied by setting the pandas.options: styler.format.formatter: default None. styler.format.na_rep: default None. styler.format.precision: default 6. styler.format.decimal: default “.”. styler.format.thousands: default None.
🌐
Stacked Turtles
kiwidamien.github.io › stylish-pandas.html
Stylish Pandas
November 6, 2019 - The default styling options don't apply to styler objects (annoyingly). If you use the format specification for a column, you will get the original formatting for all the other columns. A trick I use to get around this is to use select_dtypes to select the numeric columns, and then use a dictionary comprehension to generate a dictionary with defaults, and then override the columns I want. In code: # set ALL float columns to '${:,.2f}' formatting (including the percentage) format_dict = {col_name: '${:,.2f}' for col_name in contribution.select_dtypes(float).columns} # override the percentage column format_dict['Individual % of total'] = '{:.1%}' contribution.head().style.format(format_dict)
🌐
Pandas
pandas.pydata.org › docs › user_guide › options.html
Options and settings — pandas documentation - PyData |
[default: None] [currently: None] ... numbers. [default: 6] [currently: 6] styler.format.thousands : str, optional The character representation for thousands separator for floats, int and complex....
Find elsewhere
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 › 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
When instantiating a Styler, default formatting can be applied be setting the pandas.options: styler.format.formatter: default None. styler.format.na_rep: default None. styler.format.precision: default 6. styler.format.decimal: default “.”. styler.format.thousands: default None.
🌐
Pandas
pandas.pydata.org › pandas-docs › stable › user_guide › options.html
Options and settings — pandas 2.2.2 documentation - PyData |
February 12, 2022 - [default: None] [currently: None] ... numbers. [default: 6] [currently: 6] styler.format.thousands : str, optional The character representation for thousands separator for floats, int and complex....
🌐
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 - I see that the styler supports formatting thousands: 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 · however · s # will display 1000000 · Would it be possible to add the same to the display options?
Author   enryH
🌐
Pandas
pandas.pydata.org › docs › user_guide › style.html
Table Visualization — pandas documentation - PyData |
Additionally, the format function has a precision argument to specifically help format floats, as well as decimal and thousands separators to support other locales, an na_rep argument to display missing data, and an escape and hyperlinks arguments to help displaying safe-HTML or safe-LaTeX. The default formatter is configured to adopt pandas’ global options such as styler.format.precision option, controllable using with pd.option_context('format.precision', 2):
🌐
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.io.formats.style.Styler.html
pandas.io.formats.style.Styler — pandas 2.3.3 documentation
class pandas.io.formats.style.Styler(data, precision=None, table_styles=None, uuid=None, caption=None, table_attributes=None, cell_ids=True, na_rep=None, uuid_len=5, decimal=None, thousands=None, escape=None, formatter=None)[source]# Helps style a DataFrame or Series according to the data with HTML and CSS. ... Data to be styled - either a Series or DataFrame. ... Precision to round floats to. If not given defaults to pandas.options.styler.format.precision.
🌐
Quora
quora.com › How-do-you-format-data-within-a-Pandas-DataFrame
How to format data within a Pandas DataFrame - Quora
Additionally, the format function has a precision argument to specifically help formatting floats, as well as decimal and thousands separators to support oth ... Before adding styles it is useful to show that the Styler can distinguish the display value from the actual value.
🌐
DataScientYst
datascientyst.com › style-pandas-dataframe-like-pro-examples
Style Pandas DataFrame Like a Pro (Examples)
January 23, 2026 - with pd.option_context("display.max_rows", 1000, "display.max_columns", 50): print(pd.get_option("display.max_rows")) print(pd.get_option("display.max_columns")) To find more for Pandas options we can refer to the official documentation: Pandas options and settings · Finally we will cover several tips for styling Pandas DataFrames: don't overdo it - use styles when needed. To many colors might distract the person who will digest the information ... Share your tips as comments below the article! Thank you! API reference - pandas.io.formats.style.Styler.format
Top answer
1 of 1
3

The short answer is: no, there is no way to "apply a style permanently to a DataFrame".

Styling is typically the last step before rendering (as HTML), although it can be composed by chaining.

The result of df.style.anyfunction() is a Style object. As such, it doesn't have the methods of DataFrame, in particular as per your question, .head():

>>> type(df.style.format('${:,.2f}'))
pandas.io.formats.style.Styler

The DataFrame doesn't know anything about any Styler. Instead, it is the Styler that has a reference to the DataFrame it was originally used for.

You can take the style itself and apply it to another DataFrame (with Styler.use() and Styler.export(), see below). However, regrettably, that doesn't seem to include .format() definitions (at least in pandas=1.2.1).

It's really too bad, because otherwise, it makes it quite easy to reuse a style for another DF (or df.head() per your example):

def neg_red(x):
    return f"color: {'red' if x < 0 else 'black'}"

df = pd.DataFrame(np.random.uniform(-4, 5, (5, 3)) * 1e4)
s = df.style.format('${:,.2f}').applymap(neg_red).highlight_max()
s

Observe that: assert vars(s)['data'] is df. So as I said above, the Styler object has a reference (data) to the DataFrame it was used on initially (so that, when it is asked to render, it knows what data to render).

Now, you can use the Styler object and apply it to another DataFrame (here: df.head(3)):

df.head(3).style.use(s.export())

As you can see, all the styling functions have been carried over, except for the formatting! I would imagine this is a bug or an oversight.