import pandas as pd
pd.options.display.float_format = '${:,.2f}'.format
df = pd.DataFrame([123.4567, 234.5678, 345.6789, 456.7890],
                  index=['foo','bar','baz','quux'],
                  columns=['cost'])
print(df)

yields

        cost
foo  234.57
baz  $345.68
quux $456.79

but this only works if you want every float to be formatted with a dollar sign.

Otherwise, if you want dollar formatting for some floats only, then I think you'll have to pre-modify the dataframe (converting those floats to strings):

import pandas as pd
df = pd.DataFrame([123.4567, 234.5678, 345.6789, 456.7890],
                  index=['foo','bar','baz','quux'],
                  columns=['cost'])
df['foo'] = df['cost']
df['cost'] = df['cost'].map('${:,.2f}'.format)
print(df)

yields

         cost       foo
foo   $123.46  123.4567
bar   $234.57  234.5678
baz   $345.68  345.6789
quux  $456.79  456.7890
Answer from unutbu on Stack Overflow
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.io.formats.style.Styler.format.html
pandas.io.formats.style.Styler.format — pandas 3.0.2 documentation
Replace semi-colons with the section separator character (ASCII-245) when defining the formatting here. >>> df = pd.DataFrame({"A": [1, 0, -1]}) >>> pseudo_css = "number-format: 0§[Red](0)§-§@;" >>> filename = "formatted_file.xlsx" >>> df.style.map(lambda v: pseudo_css).to_excel(filename)
🌐
GeeksforGeeks
geeksforgeeks.org › python › formatting-integer-column-of-dataframe-in-pandas
Formatting float column of Dataframe in Pandas - GeeksforGeeks
October 3, 2025 - import pandas as pd data = {'Product': ['Laptop', 'Phone', 'Tablet', 'Desktop'], 'Price': [1200.50, 799.99, 349.75, 1500.25]} df = pd.DataFrame(data, columns=['Product', 'Price']) pd.options.display.float_format = '{:,.2f}'.format df['Price'] = df['Price'].apply(lambda x: '{:,.2f}'.format(x)) print(df) ... Large numbers can be hard to interpret.
🌐
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 - 204 """ --> 205 return self.render() 206 207 @doc( ~/.local/share/virtualenvs/covid-vaccines-xEbcGJTy/lib/python3.8/site-packages/pandas/io/formats/style.py in render(self, **kwargs) 619 self._compute() 620 # TODO: namespace all the pandas keys --> 621 d = self._translate() 622 # filter out empty styles, every cell will have a class 623 # but the list of props may just be [['', '']]. ~/.local/share/virtualenvs/covid-vaccines-xEbcGJTy/lib/python3.8/site-packages/pandas/io/formats/style.py in _translate(self) 403 "value": value, 404 "class": " ".join(cs), --> 405 "display_value": formatter(value
🌐
Stacked Turtles
kiwidamien.github.io › stylish-pandas.html
Stylish Pandas
November 6, 2019 - An excessive number of decimal places can make comparing numbers between different rows difficult. According to the Zen of Python, "readability counts". This article goes through some tricks to make your Jupyter notebook dataframes a little more readable. For those that just some standard formatting for their projects, this is what I will typically put at the beginning of my notebooks. import pandas as pd import matplotlib.pyplot as plt # Show up to 15 cols, 50 rows by default pd.set_option('display.max_cols', 15) pd.set_option('display.max_rows', 50) # Suitable default display for floats pd.options.display.float_format = '{:,.2f}'.format %matplotlib inline plt.rcParams['figure.figsize'] = (12, 10) # This one is optional -- change graphs to SVG # Only use if you don't have a lot of points/lines # on your graph.
🌐
Practical Business Python
pbpython.com › styling-pandas.html
Stylin’ with Pandas - Practical Business Python
If the number is $25 then the meaning is clear. Percentages are another useful example where formatting the output makes it simpler to understand the underlying analysis. For instance, which is quicker to understand: .05 or 5%? Using the percentage sign makes it very clear how to interpret the data. The key item to keep in mind is that styling presents the data so a human can read it but keeps the data in the same pandas data type so you can perform your normal pandas math, date or string functions.
🌐
Medium
medium.com › data-science › make-your-tables-look-glorious-2a5ddbfcc0e5
Comprehensive guide to formatting pandas DataFrames | TDS Archive
January 31, 2023 - This one has been a bit of an image-heavy article, but it’s been necessary to demonstrate how we can use pandas to format dates, absolute numbers, currencies, and ratios.
🌐
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.io.formats.style.Styler.format.html
pandas.io.formats.style.Styler.format — pandas 3.0.1 documentation
Replace semi-colons with the section separator character (ASCII-245) when defining the formatting here. >>> df = pd.DataFrame({"A": [1, 0, -1]}) >>> pseudo_css = "number-format: 0§[Red](0)§-§@;" >>> filename = "formatted_file.xlsx" >>> df.style.map(lambda v: pseudo_css).to_excel(filename)
Find elsewhere
🌐
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
>>> df = pd.DataFrame([["123"], ... \textasciicircum } \\ 2 & \textbf{\$\%\#} \\ \end{tabular} Pandas defines a number-format pseudo CSS attribute instead of the .format method to create to_excel permissible formatting....
🌐
Towards Data Science
towardsdatascience.com › home › latest › apply thousand separator (and other formatting) to pandas dataframe
Apply Thousand Separator (and Other Formatting) to Pandas Dataframe | Towards Data Science
January 28, 2025 - In this post, I’ll share with you a couple of pandas formatting tricks that deal with these common formatting problems: Insert thousand comma separators to numbers · Format numbers as percentages · Change the date/time column to the desired ...
🌐
Saturn Cloud
saturncloud.io › blog › how-to-format-numbers-in-a-python-pandas-dataframe-as-currency-in-thousands-or-millions
How to Format Numbers in a Python Pandas DataFrame as Currency in Thousands or Millions | Saturn Cloud Blog
January 6, 2024 - When working with numerical data in a DataFrame, it is often useful to format the values as currency. To format numbers in a pandas DataFrame as currency, we can use the map() method along with the format() function.
🌐
GitHub
gist.github.com › data-enhanced › 0c48586f36e8cd27ffad8fe076d62c4e
Format output of pandas describe() method · GitHub
To change the number of decimals, change the number before the f · To remove the thousands separator remove the comma · df.describe().apply(lambda s: s.apply('{:,.0f}'.format))
🌐
Pandas
pandas.pydata.org › docs › dev › reference › api › pandas.io.formats.style.Styler.format.html
pandas.io.formats.style.Styler.format — pandas documentation
Replace semi-colons with the section separator character (ASCII-245) when defining the formatting here. >>> df = pd.DataFrame({"A": [1, 0, -1]}) >>> pseudo_css = "number-format: 0§[Red](0)§-§@;" >>> filename = "formatted_file.xlsx" >>> df.style.map(lambda v: pseudo_css).to_excel(filename)
🌐
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.2 documentation
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.
🌐
Reddit
reddit.com › r/eli5_programming › eli5: help me understand pandas display.float_format
r/eli5_programming on Reddit: ELI5: Help me understand pandas display.float_format
December 1, 2022 -

So I understand how to *use* something like the below to change the way pandas prints numbers:

pd.set_option('display.float_format', '{:,}'.format)

or

pd.set_option('display.float_format', '{:,.2f}'.format)

But I don't understand what that second argument actually is. The documentation says

" 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. "

So I assume the second argument needs to be something that returns a string, but then I don't understand how this other example I see a lot works. My understanding of lambda functions is that they're anonymous functions, but how does the below return a string?

pd.set_option('display.float_format', lambda x: '%.5f' % x)

Can someone walk me through how pandas parses that second argument into a number format?

)

Relatedly, I also discovered that

def bfunc(num):
return('%.5f' % num)

actually returns a number with the format I want (essentially the lambda function above)...But why? What did the % sign do here? I thought that was for like template literals in Python/inserting variables into strings. What does .5f mean under the hood here if I do %.5f?

Top answer
1 of 2
3
aha its string formatting syntax, leaving this up for any latecomers https://python-reference.readthedocs.io/en/latest/docs/str/formatting.html
2 of 2
1
display.float_format is a Pandas option that allows you to specify the format for displaying floating point numbers. The value you set for this option should be a callable that takes a floating point number and returns a string with the desired format of the number. In the first example you provided, '{:,}'.format is a callable that takes a floating point number and formats it as a string with a thousands separator. For example, if you set display.float_format to this value and then print a Pandas series or dataframe with floating point numbers, the numbers will be formatted with a thousands separator: pd.set_option('display.float_format', '{:,}'.format) print(pd.Series([12345.67, 8901.234])) # Output: 0 12,345.67 # 1 8,901.234 In the second example you provided, '{:,.2f}'.format is a callable that takes a floating point number and formats it as a string with a thousands separator and two decimal places. For example: pd.set_option('display.float_format', '{:,.2f}'.format) print(pd.Series([12345.6789, 8901.234])) # Output: 0 12,345.68 # 1 8,901.23 In the third example you provided, lambda x: '%.5f' % x is a lambda function that takes a floating point number and formats it as a string with five decimal places. A lambda function is a small anonymous function that can take any number of arguments, but can only have one expression. In this case, the lambda function takes a floating point number x and formats it as a string using the '%.5f' format string. The % operator is used to insert a value into a string. The .5f part of the format string specifies that the number should be formatted as a float with five decimal places. For example: pd.set_option('display.float_format', lambda x: '%.5f' % x) print(pd.Series([12345.6789, 8901.234])) # Output: 0 12345.67890 # 1 8901.23400 In the fourth example you provided, bfunc is a function that takes a floating point number and formats it as a string with five decimal places. The function uses the '%.5f' format string in the same way as the lambda function above. For example: def bfunc(num): return('%.5f' % num) pd.set_option('display.float_format', bfunc) print(pd.Series([12345.6789, 8901.234])) # Output: 0 12345.67890 # 1 8901.23400 I hope this helps!
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 › dev › reference › api › pandas.set_eng_float_format.html
pandas.set_eng_float_format — pandas documentation
>>> pd.set_eng_float_format(use_eng_prefix=True) >>> df 0 0 1.000n 1 1.000m 2 1.000 3 1.000k 4 1.000M