{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Styling\n",
    "\n",
    "*New in version 0.17.1*\n",
    "\n",
    "*Provisional: This is a new feature and still under development. We'll be adding features and possibly making breaking changes in future releases. We'd love to hear your feedback.*\n",
    "\n",
    "This document is written as a Jupyter Notebook, and can be viewed or downloaded [here](https://nbviewer.ipython.org/github/pandas-dev/pandas/blob/master/doc/source/user_guide/style.ipynb).\n",
    "\n",
    "You can apply **conditional formatting**, the visual styling of a DataFrame\n",
    "depending on the data within, by using the ``DataFrame.style`` property.\n",
    "This is a property that returns a ``Styler`` object, which has\n",
    "useful methods for formatting and displaying DataFrames.\n",
    "\n",
    "The styling is accomplished using CSS.\n",
    "You write \"style functions\" that take scalars, `DataFrame`s or `Series`, and return *like-indexed* DataFrames or Series with CSS `\"attribute: value\"` pairs for the values.\n",
    "These functions can be incrementally passed to the `Styler` which collects the styles before rendering."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Building styles\n",
    "\n",
    "Pass your style functions into one of the following methods:\n",
    "\n",
    "- ``Styler.applymap``: elementwise\n",
    "- ``Styler.apply``: column-/row-/table-wise\n",
    "\n",
    "Both of those methods take a function (and some other keyword arguments) and applies your function to the DataFrame in a certain way.\n",
    "`Styler.applymap` works through the DataFrame elementwise.\n",
    "`Styler.apply` passes each column or row into your DataFrame one-at-a-time or the entire table at once, depending on the `axis` keyword argument.\n",
    "For columnwise use `axis=0`, rowwise use `axis=1`, and for the entire table at once use `axis=None`.\n",
    "\n",
    "For `Styler.applymap` your function should take a scalar and return a single string with the CSS attribute-value pair.\n",
    "\n",
    "For `Styler.apply` your function should take a Series or DataFrame (depending on the axis parameter), and return a Series or DataFrame with an identical shape where each value is a string with a CSS attribute-value pair.\n",
    "\n",
    "Let's see some examples."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "nbsphinx": "hidden"
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot\n",
    "# We have this here to trigger matplotlib's font cache stuff.\n",
    "# This cell is hidden from the output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "np.random.seed(24)\n",
    "df = pd.DataFrame({'A': np.linspace(1, 10, 10)})\n",
    "df = pd.concat([df, pd.DataFrame(np.random.randn(10, 4), columns=list('BCDE'))],\n",
    "               axis=1)\n",
    "df.iloc[0, 2] = np.nan"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here's a boring example of rendering a DataFrame, without any (visible) styles:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.style"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Note*: The `DataFrame.style` attribute is a property that returns a `Styler` object. `Styler` has a `_repr_html_` method defined on it so they are rendered automatically. If you want the actual HTML back for further processing or for writing to file call the `.render()` method which returns a string.\n",
    "\n",
    "The above output looks very similar to the standard DataFrame HTML representation. But we've done some work behind the scenes to attach CSS classes to each cell. We can view these by calling the `.render` method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.style.highlight_null().render().split('\\n')[:10]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `row0_col2` is the identifier for that particular cell. We've also prepended each row/column identifier with a UUID unique to each DataFrame so that the style from one doesn't collide with the styling from another within the same notebook or page (you can set the `uuid` if you'd like to tie together the styling of two DataFrames).\n",
    "\n",
    "When writing style functions, you take care of producing the CSS attribute / value pairs you want. Pandas matches those up with the CSS classes that identify each cell."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's write a simple style function that will color negative numbers red and positive numbers black."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def color_negative_red(val):\n",
    "    \"\"\"\n",
    "    Takes a scalar and returns a string with\n",
    "    the css property `'color: red'` for negative\n",
    "    strings, black otherwise.\n",
    "    \"\"\"\n",
    "    color = 'red' if val < 0 else 'black'\n",
    "    return 'color: %s' % color"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this case, the cell's style depends only on it's own value.\n",
    "That means we should use the `Styler.applymap` method which works elementwise."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = df.style.applymap(color_negative_red)\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notice the similarity with the standard `df.applymap`, which operates on DataFrames elementwise. We want you to be able to reuse your existing knowledge of how to interact with DataFrames.\n",
    "\n",
    "Notice also that our function returned a string containing the CSS attribute and value, separated by a colon just like in a `<style>` tag. This will be a common theme.\n",
    "\n",
    "Finally, the input shapes matched. `Styler.applymap` calls the function on each scalar input, and the function returns a scalar output."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now suppose you wanted to highlight the maximum value in each column.\n",
    "We can't use `.applymap` anymore since that operated elementwise.\n",
    "Instead, we'll turn to `.apply` which operates columnwise (or rowwise using the `axis` keyword). Later on we'll see that something like `highlight_max` is already defined on `Styler` so you wouldn't need to write this yourself."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def highlight_max(s):\n",
    "    '''\n",
    "    highlight the maximum in a Series yellow.\n",
    "    '''\n",
    "    is_max = s == s.max()\n",
    "    return ['background-color: yellow' if v else '' for v in is_max]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.style.apply(highlight_max)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this case the input is a `Series`, one column at a time.\n",
    "Notice that the output shape of `highlight_max` matches the input shape, an array with `len(s)` items."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We encourage you to use method chains to build up a style piecewise, before finally rending at the end of the chain."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.style.\\\n",
    "    applymap(color_negative_red).\\\n",
    "    apply(highlight_max)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Above we used `Styler.apply` to pass in each column one at a time.\n",
    "\n",
    "*Debugging Tip*: If you're having trouble writing your style function, try just passing it into DataFrame.apply. Internally, Styler.apply uses DataFrame.apply so the result should be the same.\n",
    "\n",
    "What if you wanted to highlight just the maximum value in the entire table?\n",
    "Use `.apply(function, axis=None)` to indicate that your function wants the entire table, not one column or row at a time. Let's try that next.\n",
    "\n",
    "We'll rewrite our `highlight-max` to handle either Series (from `.apply(axis=0 or 1)`) or DataFrames (from `.apply(axis=None)`). We'll also allow the color to be adjustable, to demonstrate that `.apply`, and `.applymap` pass along keyword arguments."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def highlight_max(data, color='yellow'):\n",
    "    '''\n",
    "    highlight the maximum in a Series or DataFrame\n",
    "    '''\n",
    "    attr = 'background-color: {}'.format(color)\n",
    "    if data.ndim == 1:  # Series from .apply(axis=0) or axis=1\n",
    "        is_max = data == data.max()\n",
    "        return [attr if v else '' for v in is_max]\n",
    "    else:  # from .apply(axis=None)\n",
    "        is_max = data == data.max().max()\n",
    "        return pd.DataFrame(np.where(is_max, attr, ''),\n",
    "                            index=data.index, columns=data.columns)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When using ``Styler.apply(func, axis=None)``, the function must return a DataFrame with the same index and column labels."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.style.apply(highlight_max, color='darkorange', axis=None)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Building Styles Summary\n",
    "\n",
    "Style functions should return strings with one or more CSS `attribute: value` delimited by semicolons. Use\n",
    "\n",
    "- `Styler.applymap(func)` for elementwise styles\n",
    "- `Styler.apply(func, axis=0)` for columnwise styles\n",
    "- `Styler.apply(func, axis=1)` for rowwise styles\n",
    "- `Styler.apply(func, axis=None)` for tablewise styles\n",
    "\n",
    "And crucially the input and output shapes of `func` must match. If `x` is the input then ``func(x).shape == x.shape``."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Finer control: slicing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Both `Styler.apply`, and `Styler.applymap` accept a `subset` keyword.\n",
    "This allows you to apply styles to specific rows or columns, without having to code that logic into your `style` function.\n",
    "\n",
    "The value passed to `subset` behaves similar to slicing a DataFrame.\n",
    "\n",
    "- A scalar is treated as a column label\n",
    "- A list (or series or numpy array)\n",
    "- A tuple is treated as `(row_indexer, column_indexer)`\n",
    "\n",
    "Consider using `pd.IndexSlice` to construct the tuple for the last one."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.style.apply(highlight_max, subset=['B', 'C', 'D'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For row and column slicing, any valid indexer to `.loc` will work."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.style.applymap(color_negative_red,\n",
    "                  subset=pd.IndexSlice[2:5, ['B', 'D']])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Only label-based slicing is supported right now, not positional.\n",
    "\n",
    "If your style function uses a `subset` or `axis` keyword argument, consider wrapping your function in a `functools.partial`, partialing out that keyword.\n",
    "\n",
    "```python\n",
    "my_func2 = functools.partial(my_func, subset=42)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Finer Control: Display Values\n",
    "\n",
    "We distinguish the *display* value from the *actual* value in `Styler`.\n",
    "To control the display value, the text is printed in each cell, use `Styler.format`. Cells can be formatted according to a [format spec string](https://docs.python.org/3/library/string.html#format-specification-mini-language) or a callable that takes a single value and returns a string."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.style.format(\"{:.2%}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Use a dictionary to format specific columns."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.style.format({'B': \"{:0<4.0f}\", 'D': '{:+.2f}'})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Or pass in a callable (or dictionary of callables) for more flexible handling."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.style.format({\"B\": lambda x: \"±{:.2f}\".format(abs(x))})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Builtin styles"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, we expect certain styling functions to be common enough that we've included a few \"built-in\" to the `Styler`, so you don't have to write them yourself."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.style.highlight_null(null_color='red')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can create \"heatmaps\" with the `background_gradient` method. These require matplotlib, and we'll use [Seaborn](https://stanford.edu/~mwaskom/software/seaborn/) to get a nice colormap."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import seaborn as sns\n",
    "\n",
    "cm = sns.light_palette(\"green\", as_cmap=True)\n",
    "\n",
    "s = df.style.background_gradient(cmap=cm)\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`Styler.background_gradient` takes the keyword arguments `low` and `high`. Roughly speaking these extend the range of your data by `low` and `high` percent so that when we convert the colors, the colormap's entire range isn't used. This is useful so that you can actually read the text still."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Uses the full color range\n",
    "df.loc[:4].style.background_gradient(cmap='viridis')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Compress the color range\n",
    "(df.loc[:4]\n",
    "    .style\n",
    "    .background_gradient(cmap='viridis', low=.5, high=0)\n",
    "    .highlight_null('red'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There's also `.highlight_min` and `.highlight_max`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.style.highlight_max(axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Use `Styler.set_properties` when the style doesn't actually depend on the values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.style.set_properties(**{'background-color': 'black',\n",
    "                           'color': 'lawngreen',\n",
    "                           'border-color': 'white'})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Bar charts"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can include \"bar charts\" in your DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.style.bar(subset=['A', 'B'], color='#d65f5f')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "New in version 0.20.0 is the ability to customize further the bar chart: You can now have the `df.style.bar` be centered on zero or midpoint value (in addition to the already existing way of having the min value at the left side of the cell), and you can pass a list of `[color_negative, color_positive]`.\n",
    "\n",
    "Here's how you can change the above with the new `align='mid'` option:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.style.bar(subset=['A', 'B'], align='mid', color=['#d65f5f', '#5fba7d'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The following example aims to give a highlight of the behavior of the new align options:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from IPython.display import HTML\n",
    "\n",
    "# Test series\n",
    "test1 = pd.Series([-100,-60,-30,-20], name='All Negative')\n",
    "test2 = pd.Series([10,20,50,100], name='All Positive')\n",
    "test3 = pd.Series([-10,-5,0,90], name='Both Pos and Neg')\n",
    "\n",
    "head = \"\"\"\n",
    "
\n",
    "    
\n",
    "         Align\n",
    "         All Negative\n",
    "         All Positive\n",
    "         Both Neg and Pos\n",
    "    
\n",
    "    
\n",
    "\n",
    "\"\"\"\n",
    "\n",
    "aligns = ['left','zero','mid']\n",
    "for align in aligns:\n",
    "    row = \" {}\".format(align)\n",
    "    for serie in [test1,test2,test3]:\n",
    "        s = serie.copy()\n",
    "        s.name=''\n",
    "        row += \" {}\".format(s.to_frame().style.bar(align=align, \n",
    "                                                           color=['#d65f5f', '#5fba7d'], \n",
    "                                                           width=100).render()) #testn['width']\n",
    "    row += '
'\n",
    "    head += row\n",
    "    \n",
    "head+= \"\"\"\n",
    "
\n",
    "
\"\"\"\n",
    "        \n",
    "\n",
    "HTML(head)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Sharing styles"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Say you have a lovely style built up for a DataFrame, and now you want to apply the same style to a second DataFrame. Export the style with `df1.style.export`, and import it on the second DataFrame with `df1.style.set`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df2 = -df\n",
    "style1 = df.style.applymap(color_negative_red)\n",
    "style1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "style2 = df2.style\n",
    "style2.use(style1.export())\n",
    "style2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notice that you're able share the styles even though they're data aware. The styles are re-evaluated on the new DataFrame they've been `use`d upon."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Other Options\n",
    "\n",
    "You've seen a few methods for data-driven styling.\n",
    "`Styler` also provides a few other options for styles that don't depend on the data.\n",
    "\n",
    "- precision\n",
    "- captions\n",
    "- table-wide styles\n",
    "- hiding the index or columns\n",
    "\n",
    "Each of these can be specified in two ways:\n",
    "\n",
    "- A keyword argument to `Styler.__init__`\n",
    "- A call to one of the `.set_` or `.hide_` methods, e.g. `.set_caption` or `.hide_columns`\n",
    "\n",
    "The best method to use depends on the context. Use the `Styler` constructor when building many styled DataFrames that should all share the same properties. For interactive use, the`.set_` and `.hide_` methods are more convenient."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Precision"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can control the precision of floats using pandas' regular `display.precision` option."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with pd.option_context('display.precision', 2):\n",
    "    html = (df.style\n",
    "              .applymap(color_negative_red)\n",
    "              .apply(highlight_max))\n",
    "html"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Or through a `set_precision` method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.style\\\n",
    "  .applymap(color_negative_red)\\\n",
    "  .apply(highlight_max)\\\n",
    "  .set_precision(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Setting the precision only affects the printed number; the full-precision values are always passed to your style functions. You can always use `df.round(2).style` if you'd prefer to round from the start."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Captions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Regular table captions can be added in a few ways."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.style.set_caption('Colormaps, with a caption.')\\\n",
    "    .background_gradient(cmap=cm)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Table styles"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The next option you have are \"table styles\".\n",
    "These are styles that apply to the table as a whole, but don't look at the data.\n",
    "Certain sytlings, including pseudo-selectors like `:hover` can only be used this way."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.display import HTML\n",
    "\n",
    "def hover(hover_color=\"#ffff99\"):\n",
    "    return dict(selector=\"tr:hover\",\n",
    "                props=[(\"background-color\", \"%s\" % hover_color)])\n",
    "\n",
    "styles = [\n",
    "    hover(),\n",
    "    dict(selector=\"th\", props=[(\"font-size\", \"150%\"),\n",
    "                               (\"text-align\", \"center\")]),\n",
    "    dict(selector=\"caption\", props=[(\"caption-side\", \"bottom\")])\n",
    "]\n",
    "html = (df.style.set_table_styles(styles)\n",
    "          .set_caption(\"Hover to highlight.\"))\n",
    "html"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`table_styles` should be a list of dictionaries.\n",
    "Each dictionary should have the `selector` and `props` keys.\n",
    "The value for `selector` should be a valid CSS selector.\n",
    "Recall that all the styles are already attached to an `id`, unique to\n",
    "each `Styler`. This selector is in addition to that `id`.\n",
    "The value for `props` should be a list of tuples of `('attribute', 'value')`.\n",
    "\n",
    "`table_styles` are extremely flexible, but not as fun to type out by hand.\n",
    "We hope to collect some useful ones either in pandas, or preferable in a new package that [builds on top](#Extensibility) the tools here."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Hiding the Index or Columns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The index can be hidden from rendering by calling `Styler.hide_index`. Columns can be hidden from rendering by calling `Styler.hide_columns` and passing in the name of a column, or a slice of columns."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.style.hide_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.style.hide_columns(['C','D'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### CSS classes\n",
    "\n",
    "Certain CSS classes are attached to cells.\n",
    "\n",
    "- Index and Column names include `index_name` and `level<k>` where `k` is its level in a MultiIndex\n",
    "- Index label cells include\n",
    "  + `row_heading`\n",
    "  + `row<n>` where `n` is the numeric position of the row\n",
    "  + `level<k>` where `k` is the level in a MultiIndex\n",
    "- Column label cells include\n",
    "  + `col_heading`\n",
    "  + `col<n>` where `n` is the numeric position of the column\n",
    "  + `level<k>` where `k` is the level in a MultiIndex\n",
    "- Blank cells include `blank`\n",
    "- Data cells include `data`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Limitations\n",
    "\n",
    "- DataFrame only `(use Series.to_frame().style)`\n",
    "- The index and columns must be unique\n",
    "- No large repr, and performance isn't great; this is intended for summary DataFrames\n",
    "- You can only style the *values*, not the index or columns\n",
    "- You can only apply styles, you can't insert new HTML entities\n",
    "\n",
    "Some of these will be addressed in the future.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Terms\n",
    "\n",
    "- Style function: a function that's passed into `Styler.apply` or `Styler.applymap` and returns values like `'css attribute: value'`\n",
    "- Builtin style functions: style functions that are methods on `Styler`\n",
    "- table style: a dictionary with the two keys `selector` and `props`. `selector` is the CSS selector that `props` will apply to. `props` is a list of `(attribute, value)` tuples. A list of table styles passed into `Styler`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Fun stuff\n",
    "\n",
    "Here are a few interesting examples.\n",
    "\n",
    "`Styler` interacts pretty well with widgets. If you're viewing this online instead of running the notebook yourself, you're missing out on interactively adjusting the color palette."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.html import widgets\n",
    "@widgets.interact\n",
    "def f(h_neg=(0, 359, 1), h_pos=(0, 359), s=(0., 99.9), l=(0., 99.9)):\n",
    "    return df.style.background_gradient(\n",
    "        cmap=sns.palettes.diverging_palette(h_neg=h_neg, h_pos=h_pos, s=s, l=l,\n",
    "                                            as_cmap=True)\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def magnify():\n",
    "    return [dict(selector=\"th\",\n",
    "                 props=[(\"font-size\", \"4pt\")]),\n",
    "            dict(selector=\"td\",\n",
    "                 props=[('padding', \"0em 0em\")]),\n",
    "            dict(selector=\"th:hover\",\n",
    "                 props=[(\"font-size\", \"12pt\")]),\n",
    "            dict(selector=\"tr:hover td:hover\",\n",
    "                 props=[('max-width', '200px'),\n",
    "                        ('font-size', '12pt')])\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.seed(25)\n",
    "cmap = cmap=sns.diverging_palette(5, 250, as_cmap=True)\n",
    "bigdf = pd.DataFrame(np.random.randn(20, 25)).cumsum()\n",
    "\n",
    "bigdf.style.background_gradient(cmap, axis=1)\\\n",
    "    .set_properties(**{'max-width': '80px', 'font-size': '1pt'})\\\n",
    "    .set_caption(\"Hover to magnify\")\\\n",
    "    .set_precision(2)\\\n",
    "    .set_table_styles(magnify())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Export to Excel\n",
    "\n",
    "*New in version 0.20.0*\n",
    "\n",
    "*Experimental: This is a new feature and still under development. We'll be adding features and possibly making breaking changes in future releases. We'd love to hear your feedback.*\n",
    "\n",
    "Some support is available for exporting styled `DataFrames` to Excel worksheets using the `OpenPyXL` or `XlsxWriter` engines. CSS2.2 properties handled include:\n",
    "\n",
    "- `background-color`\n",
    "- `border-style`, `border-width`, `border-color` and their {`top`, `right`, `bottom`, `left` variants}\n",
    "- `color`\n",
    "- `font-family`\n",
    "- `font-style`\n",
    "- `font-weight`\n",
    "- `text-align`\n",
    "- `text-decoration`\n",
    "- `vertical-align`\n",
    "- `white-space: nowrap`\n",
    "\n",
    "\n",
    "- Only CSS2 named colors and hex colors of the form `#rgb` or `#rrggbb` are currently supported.\n",
    "- The following pseudo CSS properties are also available to set excel specific style properties:\n",
    "    - `number-format`\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.style.\\\n",
    "    applymap(color_negative_red).\\\n",
    "    apply(highlight_max).\\\n",
    "    to_excel('styled.xlsx', engine='openpyxl')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A screenshot of the output:\n",
    "\n",
    "![Excel spreadsheet with styled DataFrame](../_static/style-excel.png)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Extensibility\n",
    "\n",
    "The core of pandas is, and will remain, its \"high-performance, easy-to-use data structures\".\n",
    "With that in mind, we hope that `DataFrame.style` accomplishes two goals\n",
    "\n",
    "- Provide an API that is pleasing to use interactively and is \"good enough\" for many tasks\n",
    "- Provide the foundations for dedicated libraries to build on\n",
    "\n",
    "If you build a great library on top of this, let us know and we'll [link](http://pandas.pydata.org/pandas-docs/stable/ecosystem.html) to it.\n",
    "\n",
    "### Subclassing\n",
    "\n",
    "If the default template doesn't quite suit your needs, you can subclass Styler and extend or override the template.\n",
    "We'll show an example of extending the default template to insert a custom header before each table."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from jinja2 import Environment, ChoiceLoader, FileSystemLoader\n",
    "from IPython.display import HTML\n",
    "from pandas.io.formats.style import Styler"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We'll use the following template:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"templates/myhtml.tpl\") as f:\n",
    "    print(f.read())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now that we've created a template, we need to set up a subclass of ``Styler`` that\n",
    "knows about it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class MyStyler(Styler):\n",
    "    env = Environment(\n",
    "        loader=ChoiceLoader([\n",
    "            FileSystemLoader(\"templates\"),  # contains ours\n",
    "            Styler.loader,  # the default\n",
    "        ])\n",
    "    )\n",
    "    template = env.get_template(\"myhtml.tpl\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notice that we include the original loader in our environment's loader.\n",
    "That's because we extend the original template, so the Jinja environment needs\n",
    "to be able to find it.\n",
    "\n",
    "Now we can use that custom styler. It's `__init__` takes a DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "MyStyler(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Our custom template accepts a `table_title` keyword. We can provide the value in the `.render` method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "HTML(MyStyler(df).render(table_title=\"Extending Example\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For convenience, we provide the `Styler.from_custom_template` method that does the same as the custom subclass."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "EasyStyler = Styler.from_custom_template(\"templates\", \"myhtml.tpl\")\n",
    "EasyStyler(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here's the template structure:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"templates/template_structure.html\") as f:\n",
    "    structure = f.read()\n",
    "    \n",
    "HTML(structure)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "See the template in the [GitHub repo](https://github.com/pandas-dev/pandas) for more details."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "nbsphinx": "hidden"
   },
   "outputs": [],
   "source": [
    "# Hack to get the same style in the notebook as the\n",
    "# main site. This is hidden in the docs.\n",
    "from IPython.display import HTML\n",
    "with open(\"themes/nature_with_gtoc/static/nature.css_t\") as f:\n",
    "    css = f.read()\n",
    "    \n",
    "HTML(''.format(css))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
