|
1 | 1 | {
|
2 | 2 | "cells": [
|
3 | 3 | {
|
4 |
| - "attachments": {}, |
5 | 4 | "cell_type": "markdown",
|
6 | 5 | "metadata": {},
|
7 | 6 | "source": [
|
|
352 | 351 | "\n",
|
353 | 352 | "- Using [.set_table_styles()][table] to control broader areas of the table with specified internal CSS. Although table styles allow the flexibility to add CSS selectors and properties controlling all individual parts of the table, they are unwieldy for individual cell specifications. Also, note that table styles cannot be exported to Excel. \n",
|
354 | 353 | "- Using [.set_td_classes()][td_class] to directly link either external CSS classes to your data cells or link the internal CSS classes created by [.set_table_styles()][table]. See [here](#Setting-Classes-and-Linking-to-External-CSS). These cannot be used on column header rows or indexes, and also won't export to Excel. \n",
|
355 |
| - "- Using the [.apply()][apply] and [.applymap()][applymap] functions to add direct internal CSS to specific data cells. See [here](#Styler-Functions). As of v1.4.0 there are also methods that work directly on column header rows or indexes; [.apply_index()][applyindex] and [.applymap_index()][applymapindex]. Note that only these methods add styles that will export to Excel. These methods work in a similar way to [DataFrame.apply()][dfapply] and [DataFrame.map()][dfmap].\n", |
| 354 | + "- Using the [.apply()][apply] and [.map()][map] functions to add direct internal CSS to specific data cells. See [here](#Styler-Functions). As of v1.4.0 there are also methods that work directly on column header rows or indexes; [.apply_index()][applyindex] and [.map_index()][mapindex]. Note that only these methods add styles that will export to Excel. These methods work in a similar way to [DataFrame.apply()][dfapply] and [DataFrame.map()][dfmap].\n", |
356 | 355 | "\n",
|
357 | 356 | "[table]: ../reference/api/pandas.io.formats.style.Styler.set_table_styles.rst\n",
|
358 | 357 | "[styler]: ../reference/api/pandas.io.formats.style.Styler.rst\n",
|
359 | 358 | "[td_class]: ../reference/api/pandas.io.formats.style.Styler.set_td_classes.rst\n",
|
360 | 359 | "[apply]: ../reference/api/pandas.io.formats.style.Styler.apply.rst\n",
|
361 |
| - "[applymap]: ../reference/api/pandas.io.formats.style.Styler.applymap.rst\n", |
| 360 | + "[map]: ../reference/api/pandas.io.formats.style.Styler.map.rst\n", |
362 | 361 | "[applyindex]: ../reference/api/pandas.io.formats.style.Styler.apply_index.rst\n",
|
363 |
| - "[applymapindex]: ../reference/api/pandas.io.formats.style.Styler.applymap_index.rst\n", |
| 362 | + "[mapindex]: ../reference/api/pandas.io.formats.style.Styler.map_index.rst\n", |
364 | 363 | "[dfapply]: ../reference/api/pandas.DataFrame.apply.rst\n",
|
365 | 364 | "[dfmap]: ../reference/api/pandas.DataFrame.map.rst"
|
366 | 365 | ]
|
|
565 | 564 | "\n",
|
566 | 565 | "We use the following methods to pass your style functions. Both of those methods take a function (and some other keyword arguments) and apply it to the DataFrame in a certain way, rendering CSS styles.\n",
|
567 | 566 | "\n",
|
568 |
| - "- [.applymap()][applymap] (elementwise): accepts a function that takes a single value and returns a string with the CSS attribute-value pair.\n", |
| 567 | + "- [.map()][map] (elementwise): accepts a function that takes a single value and returns a string with the CSS attribute-value pair.\n", |
569 | 568 | "- [.apply()][apply] (column-/row-/table-wise): accepts a function that takes a Series or DataFrame and returns a Series, DataFrame, or numpy array with an identical shape where each element is a string with a CSS attribute-value pair. This method passes each column or row of your DataFrame one-at-a-time or the entire table at once, depending on the `axis` keyword argument. For columnwise use `axis=0`, rowwise use `axis=1`, and for the entire table at once use `axis=None`.\n",
|
570 | 569 | "\n",
|
571 | 570 | "This method is powerful for applying multiple, complex logic to data cells. We create a new DataFrame to demonstrate this.\n",
|
572 | 571 | "\n",
|
573 | 572 | "[apply]: ../reference/api/pandas.io.formats.style.Styler.apply.rst\n",
|
574 |
| - "[applymap]: ../reference/api/pandas.io.formats.style.Styler.applymap.rst" |
| 573 | + "[map]: ../reference/api/pandas.io.formats.style.Styler.map.rst" |
575 | 574 | ]
|
576 | 575 | },
|
577 | 576 | {
|
|
589 | 588 | "cell_type": "markdown",
|
590 | 589 | "metadata": {},
|
591 | 590 | "source": [
|
592 |
| - "For example we can build a function that colors text if it is negative, and chain this with a function that partially fades cells of negligible value. Since this looks at each element in turn we use ``applymap``." |
| 591 | + "For example we can build a function that colors text if it is negative, and chain this with a function that partially fades cells of negligible value. Since this looks at each element in turn we use ``map``." |
593 | 592 | ]
|
594 | 593 | },
|
595 | 594 | {
|
|
600 | 599 | "source": [
|
601 | 600 | "def style_negative(v, props=''):\n",
|
602 | 601 | " return props if v < 0 else None\n",
|
603 |
| - "s2 = df2.style.applymap(style_negative, props='color:red;')\\\n", |
604 |
| - " .applymap(lambda v: 'opacity: 20%;' if (v < 0.3) and (v > -0.3) else None)\n", |
| 602 | + "s2 = df2.style.map(style_negative, props='color:red;')\\\n", |
| 603 | + " .map(lambda v: 'opacity: 20%;' if (v < 0.3) and (v > -0.3) else None)\n", |
605 | 604 | "s2"
|
606 | 605 | ]
|
607 | 606 | },
|
|
699 | 698 | "\n",
|
700 | 699 | "Similar application is achieved for headers by using:\n",
|
701 | 700 | " \n",
|
702 |
| - "- [.applymap_index()][applymapindex] (elementwise): accepts a function that takes a single value and returns a string with the CSS attribute-value pair.\n", |
| 701 | + "- [.map_index()][mapindex] (elementwise): accepts a function that takes a single value and returns a string with the CSS attribute-value pair.\n", |
703 | 702 | "- [.apply_index()][applyindex] (level-wise): accepts a function that takes a Series and returns a Series, or numpy array with an identical shape where each element is a string with a CSS attribute-value pair. This method passes each level of your Index one-at-a-time. To style the index use `axis=0` and to style the column headers use `axis=1`.\n",
|
704 | 703 | "\n",
|
705 | 704 | "You can select a `level` of a `MultiIndex` but currently no similar `subset` application is available for these methods.\n",
|
706 | 705 | "\n",
|
707 | 706 | "[applyindex]: ../reference/api/pandas.io.formats.style.Styler.apply_index.rst\n",
|
708 |
| - "[applymapindex]: ../reference/api/pandas.io.formats.style.Styler.applymap_index.rst" |
| 707 | + "[mapindex]: ../reference/api/pandas.io.formats.style.Styler.map_index.rst" |
709 | 708 | ]
|
710 | 709 | },
|
711 | 710 | {
|
|
714 | 713 | "metadata": {},
|
715 | 714 | "outputs": [],
|
716 | 715 | "source": [
|
717 |
| - "s2.applymap_index(lambda v: \"color:pink;\" if v>4 else \"color:darkblue;\", axis=0)\n", |
| 716 | + "s2.map_index(lambda v: \"color:pink;\" if v>4 else \"color:darkblue;\", axis=0)\n", |
718 | 717 | "s2.apply_index(lambda s: np.where(s.isin([\"A\", \"B\"]), \"color:pink;\", \"color:darkblue;\"), axis=1)"
|
719 | 718 | ]
|
720 | 719 | },
|
|
831 | 830 | "source": [
|
832 | 831 | "## Finer Control with Slicing\n",
|
833 | 832 | "\n",
|
834 |
| - "The examples we have shown so far for the `Styler.apply` and `Styler.applymap` functions have not demonstrated the use of the ``subset`` argument. This is a useful argument which permits a lot of flexibility: it allows you to apply styles to specific rows or columns, without having to code that logic into your `style` function.\n", |
| 833 | + "The examples we have shown so far for the `Styler.apply` and `Styler.map` functions have not demonstrated the use of the ``subset`` argument. This is a useful argument which permits a lot of flexibility: it allows you to apply styles to specific rows or columns, without having to code that logic into your `style` function.\n", |
835 | 834 | "\n",
|
836 | 835 | "The value passed to `subset` behaves similar to slicing a DataFrame;\n",
|
837 | 836 | "\n",
|
|
1034 | 1033 | "outputs": [],
|
1035 | 1034 | "source": [
|
1036 | 1035 | "props = 'font-family: \"Times New Roman\", Times, serif; color: #e83e8c; font-size:1.3em;'\n",
|
1037 |
| - "df4.style.applymap(lambda x: props, subset=[1])" |
| 1036 | + "df4.style.map(lambda x: props, subset=[1])" |
1038 | 1037 | ]
|
1039 | 1038 | },
|
1040 | 1039 | {
|
|
1321 | 1320 | "source": [
|
1322 | 1321 | "### Set properties\n",
|
1323 | 1322 | "\n",
|
1324 |
| - "Use `Styler.set_properties` when the style doesn't actually depend on the values. This is just a simple wrapper for `.applymap` where the function returns the same properties for all cells." |
| 1323 | + "Use `Styler.set_properties` when the style doesn't actually depend on the values. This is just a simple wrapper for `.map` where the function returns the same properties for all cells." |
1325 | 1324 | ]
|
1326 | 1325 | },
|
1327 | 1326 | {
|
|
1465 | 1464 | "outputs": [],
|
1466 | 1465 | "source": [
|
1467 | 1466 | "style1 = df2.style\\\n",
|
1468 |
| - " .applymap(style_negative, props='color:red;')\\\n", |
1469 |
| - " .applymap(lambda v: 'opacity: 20%;' if (v < 0.3) and (v > -0.3) else None)\\\n", |
| 1467 | + " .map(style_negative, props='color:red;')\\\n", |
| 1468 | + " .map(lambda v: 'opacity: 20%;' if (v < 0.3) and (v > -0.3) else None)\\\n", |
1470 | 1469 | " .set_table_styles([{\"selector\": \"th\", \"props\": \"color: blue;\"}])\\\n",
|
1471 | 1470 | " .hide(axis=\"index\")\n",
|
1472 | 1471 | "style1"
|
|
1683 | 1682 | " - `number-format`\n",
|
1684 | 1683 | " - `border-style` (for Excel-specific styles: \"hair\", \"mediumDashDot\", \"dashDotDot\", \"mediumDashDotDot\", \"dashDot\", \"slantDashDot\", or \"mediumDashed\")\n",
|
1685 | 1684 | "\n",
|
1686 |
| - "Table level styles, and data cell CSS-classes are not included in the export to Excel: individual cells must have their properties mapped by the `Styler.apply` and/or `Styler.applymap` methods." |
| 1685 | + "Table level styles, and data cell CSS-classes are not included in the export to Excel: individual cells must have their properties mapped by the `Styler.apply` and/or `Styler.map` methods." |
1687 | 1686 | ]
|
1688 | 1687 | },
|
1689 | 1688 | {
|
|
1693 | 1692 | "outputs": [],
|
1694 | 1693 | "source": [
|
1695 | 1694 | "df2.style.\\\n",
|
1696 |
| - " applymap(style_negative, props='color:red;').\\\n", |
| 1695 | + " map(style_negative, props='color:red;').\\\n", |
1697 | 1696 | " highlight_max(axis=0).\\\n",
|
1698 | 1697 | " to_excel('styled.xlsx', engine='openpyxl')"
|
1699 | 1698 | ]
|
|
1783 | 1782 | "outputs": [],
|
1784 | 1783 | "source": [
|
1785 | 1784 | "df4 = pd.DataFrame([['text']])\n",
|
1786 |
| - "df4.style.applymap(lambda x: 'color:green;')\\\n", |
1787 |
| - " .applymap(lambda x: 'color:red;')" |
| 1785 | + "df4.style.map(lambda x: 'color:green;')\\\n", |
| 1786 | + " .map(lambda x: 'color:red;')" |
1788 | 1787 | ]
|
1789 | 1788 | },
|
1790 | 1789 | {
|
|
1793 | 1792 | "metadata": {},
|
1794 | 1793 | "outputs": [],
|
1795 | 1794 | "source": [
|
1796 |
| - "df4.style.applymap(lambda x: 'color:red;')\\\n", |
1797 |
| - " .applymap(lambda x: 'color:green;')" |
| 1795 | + "df4.style.map(lambda x: 'color:red;')\\\n", |
| 1796 | + " .map(lambda x: 'color:green;')" |
1798 | 1797 | ]
|
1799 | 1798 | },
|
1800 | 1799 | {
|
|
1821 | 1820 | "source": [
|
1822 | 1821 | "df4.style.set_uuid('a_')\\\n",
|
1823 | 1822 | " .set_table_styles([{'selector': 'td', 'props': 'color:red;'}])\\\n",
|
1824 |
| - " .applymap(lambda x: 'color:green;')" |
| 1823 | + " .map(lambda x: 'color:green;')" |
1825 | 1824 | ]
|
1826 | 1825 | },
|
1827 | 1826 | {
|
|
1840 | 1839 | "df4.style.set_uuid('b_')\\\n",
|
1841 | 1840 | " .set_table_styles([{'selector': 'td', 'props': 'color:red;'},\n",
|
1842 | 1841 | " {'selector': '.cls-1', 'props': 'color:blue;'}])\\\n",
|
1843 |
| - " .applymap(lambda x: 'color:green;')\\\n", |
| 1842 | + " .map(lambda x: 'color:green;')\\\n", |
1844 | 1843 | " .set_td_classes(pd.DataFrame([['cls-1']]))"
|
1845 | 1844 | ]
|
1846 | 1845 | },
|
|
1861 | 1860 | " .set_table_styles([{'selector': 'td', 'props': 'color:red;'},\n",
|
1862 | 1861 | " {'selector': '.cls-1', 'props': 'color:blue;'},\n",
|
1863 | 1862 | " {'selector': 'td.data', 'props': 'color:yellow;'}])\\\n",
|
1864 |
| - " .applymap(lambda x: 'color:green;')\\\n", |
| 1863 | + " .map(lambda x: 'color:green;')\\\n", |
1865 | 1864 | " .set_td_classes(pd.DataFrame([['cls-1']]))"
|
1866 | 1865 | ]
|
1867 | 1866 | },
|
|
1884 | 1883 | " .set_table_styles([{'selector': 'td', 'props': 'color:red;'},\n",
|
1885 | 1884 | " {'selector': '.cls-1', 'props': 'color:blue;'},\n",
|
1886 | 1885 | " {'selector': 'td.data', 'props': 'color:yellow;'}])\\\n",
|
1887 |
| - " .applymap(lambda x: 'color:green !important;')\\\n", |
| 1886 | + " .map(lambda x: 'color:green !important;')\\\n", |
1888 | 1887 | " .set_td_classes(pd.DataFrame([['cls-1']]))"
|
1889 | 1888 | ]
|
1890 | 1889 | },
|
|
0 commit comments