atter  ` with HTML ` escape  ` and ` na_rep  ` .

    
    
    >>> df = pd.DataFrame([['<div></div>', '"A&B"', None]])
    >>> s = df.style.format(
    ...     '<a href="a.com/{0}">{0}</a>', escape="html", na_rep="NA"
    ...     )
    >>> s.to_html()  
    ...
    <td .. ><a href="a.com/&lt;div&gt;&lt;/div&gt;">&lt;div&gt;&lt;/div&gt;</a></td>
    <td .. ><a href="a.com/&#34;A&amp;B&#34;">&#34;A&amp;B&#34;</a></td>
    <td .. >NA</td>
    ...
    

Using a ` formatter  ` with ` escape  ` in âlatexâ mode.

    
    
    >>> df = pd.DataFrame([["123"], ["~ ^"], ["$%#"]])
    >>> df.style.format("\\textbf{{{}}}", escape="latex").to_latex()
    ...  
    \begin{tabular}{ll}
     & 0 \\
    0 & \textbf{123} \\
    1 & \textbf{\textasciitilde \space \textasciicircum } \\
    2 & \textbf{\$\%\#} \\
    \end{tabular}
    

Applying ` escape  ` in âlatex-mathâ mode. In the example below we enter
math mode using the character ` $  ` .

    
    
    >>> df = pd.DataFrame([[r"$\sum_{i=1}^{10} a_i$ a~b $\alpha \
    ...     = \frac{\beta}{\zeta^2}$"], ["%#^ $ \$x^2 $"]])
    >>> df.style.format(escape="latex-math").to_latex()
    ...  
    \begin{tabular}{ll}
     & 0 \\
    0 & $\sum_{i=1}^{10} a_i$ a\textasciitilde b $\alpha = \frac{\beta}{\zeta^2}$ \\
    1 & \%\#\textasciicircum \space $ \$x^2 $ \\
    \end{tabular}
    

We can use the character ` \(  ` to enter math mode and the character ` \)  `
to close math mode.

    
    
    >>> df = pd.DataFrame([[r"\(\sum_{i=1}^{10} a_i\) a~b \(\alpha \
    ...     = \frac{\beta}{\zeta^2}\)"], ["%#^ \( \$x^2 \)"]])
    >>> df.style.format(escape="latex-math").to_latex()
    ...  
    \begin{tabular}{ll}
     & 0 \\
    0 & \(\sum_{i=1}^{10} a_i\) a\textasciitilde b \(\alpha
    = \frac{\beta}{\zeta^2}\) \\
    1 & \%\#\textasciicircum \space \( \$x^2 \) \\
    \end{tabular}
    

If we have in one DataFrame cell a combination of both shorthands for math
formulas, the shorthand with the sign ` $  ` will be applied.

    
    
    >>> df = pd.DataFrame([[r"\( x^2 \)  $x^2$"], \
    ...     [r"$\frac{\beta}{\zeta}$ \(\frac{\beta}{\zeta}\)"]])
    >>> df.style.format(escape="latex-math").to_latex()
    ...  
    \begin{tabular}{ll}
     & 0 \\
    0 & \textbackslash ( x\textasciicircum 2 \textbackslash )  $x^2$ \\
    1 & $\frac{\beta}{\zeta}$ \textbackslash (\textbackslash
    frac\{\textbackslash beta\}\{\textbackslash zeta\}\textbackslash ) \\
    \end{tabular}
    

Pandas defines a  number-format  pseudo CSS attribute instead of the  .format
method to create  to_excel  permissible formatting. Note that semi-colons are
CSS protected characters but used as separators in Excelâs format string.
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) 
    

![../../_images/format_excel_css.png](../../_images/format_excel_css.png)

[ __ previous  pandas.io.formats.style.Styler.map_index
](pandas.io.formats.style.Styler.map_index.html "previous page") [ next
pandas.io.formats.style.Styler.format_index  __
](pandas.io.formats.style.Styler.format_index.html "next page")

__ On this page

  * ` Styler.format()  `

[ __ Show Source
](../../_sources/reference/api/pandas.io.formats.style.Styler.format.rst.txt)

© 2024, pandas via [ NumFOCUS, Inc. ](https://numfocus.org) Hosted by [
OVHcloud ](https://www.ovhcloud.com) .

Created using [ Sphinx ](https://www.sphinx-doc.org/) 7.2.6.  

Built with the [ PyData Sphinx Theme ](https://pydata-sphinx-
theme.readthedocs.io/en/stable/index.html) 0.14.4.