|
150 | 150 | "\n",
|
151 | 151 | "### Formatting Values\n",
|
152 | 152 | "\n",
|
153 |
| - "Before adding styles it is useful to show that the [Styler][styler] can distinguish the *display* value from the *actual* value. To control the display value, the text is printed in each cell, and we can use the [.format()][formatfunc] method to manipulate this according to a [format spec string][format] or a callable that takes a single value and returns a string. It is possible to define this for the whole table or for individual columns. \n", |
| 153 | + "Before adding styles it is useful to show that the [Styler][styler] can distinguish the *display* value from the *actual* value, in both datavlaues and index or columns headers. To control the display value, the text is printed in each cell as string, and we can use the [.format()][formatfunc] and [.format_index()][formatfuncindex] methods to manipulate this according to a [format spec string][format] or a callable that takes a single value and returns a string. It is possible to define this for the whole table, or index, or for individual columns, or MultiIndex levels. \n", |
154 | 154 | "\n",
|
155 |
| - "Additionally, the format function has a **precision** argument to specifically help formatting floats, as well as **decimal** and **thousands** separators to support other locales, an **na_rep** argument to display missing data, and an **escape** argument to help displaying safe-HTML or safe-LaTeX. The default formatter is configured to adopt pandas' regular `display.precision` option, controllable using `with pd.option_context('display.precision', 2):`\n", |
156 |
| - "\n", |
157 |
| - "Here is an example of using the multiple options to control the formatting generally and with specific column formatters.\n", |
| 155 | + "Additionally, the format function has a **precision** argument to specifically help formatting floats, as well as **decimal** and **thousands** separators to support other locales, an **na_rep** argument to display missing data, and an **escape** argument to help displaying safe-HTML or safe-LaTeX. The default formatter is configured to adopt pandas' regular `display.precision` option, controllable using `with pd.option_context('display.precision', 2):` \n", |
158 | 156 | "\n",
|
159 | 157 | "[styler]: ../reference/api/pandas.io.formats.style.Styler.rst\n",
|
160 | 158 | "[format]: https://docs.python.org/3/library/string.html#format-specification-mini-language\n",
|
161 |
| - "[formatfunc]: ../reference/api/pandas.io.formats.style.Styler.format.rst" |
| 159 | + "[formatfunc]: ../reference/api/pandas.io.formats.style.Styler.format.rst\n", |
| 160 | + "[formatfuncindex]: ../reference/api/pandas.io.formats.style.Styler.format_index.rst" |
162 | 161 | ]
|
163 | 162 | },
|
164 | 163 | {
|
|
173 | 172 | " })"
|
174 | 173 | ]
|
175 | 174 | },
|
| 175 | + { |
| 176 | + "cell_type": "markdown", |
| 177 | + "metadata": {}, |
| 178 | + "source": [ |
| 179 | + "Using Styler to manipulate the display is a useful feature because maintaining the indexing and datavalues for other purposes gives greater control. You do not have to overwrite your DataFrame to display it how you like. Here is an example of using the formatting functions whilst still relying on the underlying data for indexing and calculations." |
| 180 | + ] |
| 181 | + }, |
| 182 | + { |
| 183 | + "cell_type": "code", |
| 184 | + "execution_count": null, |
| 185 | + "metadata": {}, |
| 186 | + "outputs": [], |
| 187 | + "source": [ |
| 188 | + "weather_df = pd.DataFrame(np.random.rand(10,2)*5, \n", |
| 189 | + " index=pd.date_range(start=\"2021-01-01\", periods=10),\n", |
| 190 | + " columns=[\"Tokyo\", \"Beijing\"])\n", |
| 191 | + "\n", |
| 192 | + "def rain_condition(v): \n", |
| 193 | + " if v < 1.75:\n", |
| 194 | + " return \"Dry\"\n", |
| 195 | + " elif v < 2.75:\n", |
| 196 | + " return \"Rain\"\n", |
| 197 | + " return \"Heavy Rain\"\n", |
| 198 | + "\n", |
| 199 | + "def make_pretty(styler):\n", |
| 200 | + " styler.set_caption(\"Weather Conditions\")\n", |
| 201 | + " styler.format(rain_condition)\n", |
| 202 | + " styler.format_index(lambda v: v.strftime(\"%A\"))\n", |
| 203 | + " styler.background_gradient(axis=None, vmin=1, vmax=5, cmap=\"YlGnBu\")\n", |
| 204 | + " return styler\n", |
| 205 | + "\n", |
| 206 | + "weather_df" |
| 207 | + ] |
| 208 | + }, |
| 209 | + { |
| 210 | + "cell_type": "code", |
| 211 | + "execution_count": null, |
| 212 | + "metadata": {}, |
| 213 | + "outputs": [], |
| 214 | + "source": [ |
| 215 | + "weather_df.loc[\"2021-01-04\":\"2021-01-08\"].style.pipe(make_pretty)" |
| 216 | + ] |
| 217 | + }, |
176 | 218 | {
|
177 | 219 | "cell_type": "markdown",
|
178 | 220 | "metadata": {},
|
|
187 | 229 | "\n",
|
188 | 230 | "Hiding does not change the integer arrangement of CSS classes, e.g. hiding the first two columns of a DataFrame means the column class indexing will start at `col2`, since `col0` and `col1` are simply ignored.\n",
|
189 | 231 | "\n",
|
190 |
| - "We can update our `Styler` object to hide some data and format the values.\n", |
| 232 | + "We can update our `Styler` object from before to hide some data and format the values.\n", |
191 | 233 | "\n",
|
192 | 234 | "[hideidx]: ../reference/api/pandas.io.formats.style.Styler.hide_index.rst\n",
|
193 | 235 | "[hidecols]: ../reference/api/pandas.io.formats.style.Styler.hide_columns.rst"
|
|
1974 | 2016 | }
|
1975 | 2017 | ],
|
1976 | 2018 | "metadata": {
|
1977 |
| - "celltoolbar": "Edit Metadata", |
1978 | 2019 | "kernelspec": {
|
1979 | 2020 | "display_name": "Python 3",
|
1980 | 2021 | "language": "python",
|
|
0 commit comments