@@ -1063,33 +1063,33 @@ def format(
1063
1063
1064
1064
Using a ``formatter`` specification on consistent column dtypes
1065
1065
1066
- >>> df.style.format('{:.2f}', na_rep='MISS', subset=[0,1]) # doctest: +SKIP
1066
+ >>> df.style.format('{:.2f}', na_rep='MISS', subset=[0, 1]) # doctest: +SKIP
1067
1067
0 1 2
1068
1068
0 MISS 1.00 A
1069
1069
1 2.00 MISS 3.000000
1070
1070
1071
1071
Using the default ``formatter`` for unspecified columns
1072
1072
1073
- >>> df.style.format({0: '{:.2f}', 1: '£ {:.1f}'}, na_rep='MISS', precision=1)
1074
- ... # doctest: +SKIP
1073
+ >>> df.style.format({0: '{:.2f}', 1: '£ {:.1f}'},
1074
+ ... na_rep='MISS', precision=1) # doctest: +SKIP
1075
1075
0 1 2
1076
1076
0 MISS £ 1.0 A
1077
1077
1 2.00 MISS 3.0
1078
1078
1079
1079
Multiple ``na_rep`` or ``precision`` specifications under the default
1080
1080
``formatter``.
1081
1081
1082
- >>> (df.style.format(na_rep='MISS', precision=1, subset=[0])
1083
- ... .format( na_rep='PASS', precision=2, subset=[1, 2])) # doctest: +SKIP
1082
+ >>> (df.style.format(na_rep='MISS', precision=1, subset=[0]).format(
1083
+ ... na_rep='PASS', precision=2, subset=[1, 2])) # doctest: +SKIP
1084
1084
0 1 2
1085
1085
0 MISS 1.00 A
1086
1086
1 2.0 PASS 3.00
1087
1087
1088
1088
Using a callable ``formatter`` function.
1089
1089
1090
1090
>>> func = lambda s: 'STRING' if isinstance(s, str) else 'FLOAT'
1091
- >>> df.style.format({0: '{:.1f}', 2: func}, precision=4, na_rep='MISS')
1092
- ... # doctest: +SKIP
1091
+ >>> df.style.format({0: '{:.1f}', 2: func},
1092
+ ... precision=4, na_rep='MISS') # doctest: +SKIP
1093
1093
0 1 2
1094
1094
0 MISS 1.0000 STRING
1095
1095
1 2.0 MISS FLOAT
@@ -1098,8 +1098,7 @@ def format(
1098
1098
1099
1099
>>> df = pd.DataFrame([['<div></div>', '"A&B"', None]])
1100
1100
>>> s = df.style.format(
1101
- ... '<a href="a.com/{0}">{0}</a>', escape="html", na_rep="NA"
1102
- ... )
1101
+ ... '<a href="a.com/{0}">{0}</a>', escape="html", na_rep="NA")
1103
1102
>>> s.to_html() # doctest: +SKIP
1104
1103
...
1105
1104
<td .. ><a href="a.com/<div></div>"><div></div></a></td>
@@ -1110,8 +1109,8 @@ def format(
1110
1109
Using a ``formatter`` with ``escape`` in 'latex' mode.
1111
1110
1112
1111
>>> df = pd.DataFrame([["123"], ["~ ^"], ["$%#"]])
1113
- >>> df.style.format("\\textbf{{{}}}", escape="latex").to_latex()
1114
- ... # doctest: +SKIP
1112
+ >>> df.style.format("\\textbf{{{}}}",
1113
+ ... escape="latex").to_latex() # doctest: +SKIP
1115
1114
\begin{tabular}{ll}
1116
1115
& 0 \\
1117
1116
0 & \textbf{123} \\
@@ -1122,10 +1121,10 @@ def format(
1122
1121
Applying ``escape`` in 'latex-math' mode. In the example below
1123
1122
we enter math mode using the character ``$``.
1124
1123
1125
- >>> df = pd.DataFrame([[r"$\sum_{i=1}^{10} a_i$ a~b $\alpha \
1126
- ... = \ frac{\beta}{\zeta^2}$"], ["%#^ $ \$x^2 $"]])
1127
- >>> df.style.format(escape="latex-math").to_latex( )
1128
- ... # doctest: +SKIP
1124
+ >>> df = pd.DataFrame([
1125
+ ... [r"$\sum_{i=1}^{10} a_i$ a~b $\alpha = \ frac{\beta}{\zeta^2}$"],
1126
+ ... [r"%#^ $ \$x^2 $"]] )
1127
+ >>> df.style.format(escape="latex-math").to_latex() # doctest: +SKIP
1129
1128
\begin{tabular}{ll}
1130
1129
& 0 \\
1131
1130
0 & $\sum_{i=1}^{10} a_i$ a\textasciitilde b $\alpha = \frac{\beta}{\zeta^2}$ \\
@@ -1135,10 +1134,10 @@ def format(
1135
1134
We can use the character ``\(`` to enter math mode and the character ``\)``
1136
1135
to close math mode.
1137
1136
1138
- >>> df = pd.DataFrame([[r"\(\sum_{i=1}^{10} a_i\) a~b \(\alpha \
1139
- ... = \frac{\beta}{\zeta^2}\)"], ["%#^ \( \$x^2 \)"]])
1140
- >>> df.style.format(escape="latex-math").to_latex( )
1141
- ... # doctest: +SKIP
1137
+ >>> df = pd.DataFrame([
1138
+ ... [r"\(\sum_{i=1}^{10} a_i\) a~b \(\alpha = \frac{\beta}{\zeta^2} \)"],
1139
+ ... [r"%#^ \( \$x^2 \)"]] )
1140
+ >>> df.style.format(escape="latex-math").to_latex() # doctest: +SKIP
1142
1141
\begin{tabular}{ll}
1143
1142
& 0 \\
1144
1143
0 & \(\sum_{i=1}^{10} a_i\) a\textasciitilde b \(\alpha
@@ -1149,10 +1148,10 @@ def format(
1149
1148
If we have in one DataFrame cell a combination of both shorthands
1150
1149
for math formulas, the shorthand with the sign ``$`` will be applied.
1151
1150
1152
- >>> df = pd.DataFrame([[r"\( x^2 \) $x^2$"], \
1151
+ >>> df = pd.DataFrame([
1152
+ ... [r"\( x^2 \) $x^2$"],
1153
1153
... [r"$\frac{\beta}{\zeta}$ \(\frac{\beta}{\zeta}\)"]])
1154
- >>> df.style.format(escape="latex-math").to_latex()
1155
- ... # doctest: +SKIP
1154
+ >>> df.style.format(escape="latex-math").to_latex() # doctest: +SKIP
1156
1155
\begin{tabular}{ll}
1157
1156
& 0 \\
1158
1157
0 & \textbackslash ( x\textasciicircum 2 \textbackslash ) $x^2$ \\
@@ -1169,7 +1168,7 @@ def format(
1169
1168
>>> df = pd.DataFrame({"A": [1, 0, -1]})
1170
1169
>>> pseudo_css = "number-format: 0§[Red](0)§-§@;"
1171
1170
>>> filename = "formatted_file.xlsx"
1172
- >>> df.style.map(lambda v: pseudo_css).to_excel(filename) # doctest: +SKIP
1171
+ >>> df.style.map(lambda v: pseudo_css).to_excel(filename) # doctest: +SKIP
1173
1172
1174
1173
.. figure:: ../../_static/style/format_excel_css.png
1175
1174
"""
0 commit comments