|
140 | 140 | "metadata": {}, |
141 | 141 | "outputs": [], |
142 | 142 | "source": [ |
143 | | - "s = df.style.set_table_attributes('class=\"table-cls\"')\n", |
144 | | - "cls = pd.DataFrame(data=[['cls1', None], ['cls3', 'cls2 cls3']], index=[0,2], columns=['A', 'C'])\n", |
145 | | - "s.set_td_classes(cls)" |
| 143 | + "css_classes = pd.DataFrame(data=[['cls1', None], ['cls3', 'cls2 cls3']], index=[0,2], columns=['A', 'C'])\n", |
| 144 | + "df.style.\\\n", |
| 145 | + " set_table_attributes('class=\"table-cls\"').\\\n", |
| 146 | + " set_td_classes(css_classes)" |
146 | 147 | ] |
147 | 148 | }, |
148 | 149 | { |
|
314 | 315 | "outputs": [], |
315 | 316 | "source": [ |
316 | 317 | "def color_negative_red(val):\n", |
317 | | - " \"\"\"\n", |
318 | | - " Takes a scalar and returns a string with\n", |
319 | | - " the css property `'color: red'` for negative\n", |
320 | | - " strings, black otherwise.\n", |
321 | | - " \"\"\"\n", |
322 | | - " color = 'red' if val < 0 else 'black'\n", |
323 | | - " return 'color: %s' % color" |
| 318 | + " \"\"\"Color negative scalars red.\"\"\"\n", |
| 319 | + " css = 'color: red;'\n", |
| 320 | + " if val < 0: return css\n", |
| 321 | + " return None" |
324 | 322 | ] |
325 | 323 | }, |
326 | 324 | { |
|
368 | 366 | "outputs": [], |
369 | 367 | "source": [ |
370 | 368 | "def highlight_max(s):\n", |
371 | | - " '''\n", |
372 | | - " highlight the maximum in a Series yellow.\n", |
373 | | - " '''\n", |
374 | | - " is_max = s == s.max()\n", |
375 | | - " return ['background-color: yellow' if v else '' for v in is_max]" |
| 369 | + " \"\"\"Highlight the maximum in a Series bold-orange.\"\"\"\n", |
| 370 | + " css = 'background-color: orange; font-weight: bold;'\n", |
| 371 | + " return np.where(s == np.nanmax(s.values), css, None)" |
376 | 372 | ] |
377 | 373 | }, |
378 | 374 | { |
|
384 | 380 | "df.style.apply(highlight_max)" |
385 | 381 | ] |
386 | 382 | }, |
| 383 | + { |
| 384 | + "cell_type": "code", |
| 385 | + "execution_count": null, |
| 386 | + "metadata": {}, |
| 387 | + "outputs": [], |
| 388 | + "source": [ |
| 389 | + "df.style.apply(highlight_max, axis=1)" |
| 390 | + ] |
| 391 | + }, |
387 | 392 | { |
388 | 393 | "cell_type": "markdown", |
389 | 394 | "metadata": {}, |
390 | 395 | "source": [ |
391 | | - "In this case the input is a `Series`, one column at a time.\n", |
| 396 | + "In this case the input is a `Series`, one column (or row) at a time.\n", |
392 | 397 | "Notice that the output shape of `highlight_max` matches the input shape, an array with `len(s)` items." |
393 | 398 | ] |
394 | 399 | }, |
|
406 | 411 | "outputs": [], |
407 | 412 | "source": [ |
408 | 413 | "def compare_col(s, comparator=None):\n", |
409 | | - " attr = 'background-color: #00BFFF;'\n", |
410 | | - " return np.where(s < comparator, attr, '')" |
| 414 | + " css = 'background-color: #00BFFF;'\n", |
| 415 | + " return np.where(s < comparator, css, None)" |
411 | 416 | ] |
412 | 417 | }, |
413 | 418 | { |
|
442 | 447 | "cell_type": "markdown", |
443 | 448 | "metadata": {}, |
444 | 449 | "source": [ |
445 | | - "Above we used `Styler.apply` to pass in each column one at a time.\n", |
| 450 | + "Above we used `Styler.apply` to pass in each column (or row) one at a time.\n", |
446 | 451 | "\n", |
447 | 452 | "<span style=\"background-color: #DEDEBE\">*Debugging Tip*: If you're having trouble writing your style function, try just passing it into <code style=\"background-color: #DEDEBE\">DataFrame.apply</code>. Internally, <code style=\"background-color: #DEDEBE\">Styler.apply</code> uses <code style=\"background-color: #DEDEBE\">DataFrame.apply</code> so the result should be the same.</span>\n", |
448 | 453 | "\n", |
449 | 454 | "What if you wanted to highlight just the maximum value in the entire table?\n", |
450 | | - "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", |
451 | | - "\n", |
452 | | - "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." |
453 | | - ] |
454 | | - }, |
455 | | - { |
456 | | - "cell_type": "code", |
457 | | - "execution_count": null, |
458 | | - "metadata": {}, |
459 | | - "outputs": [], |
460 | | - "source": [ |
461 | | - "def highlight_max(data, color='yellow'):\n", |
462 | | - " '''\n", |
463 | | - " highlight the maximum in a Series or DataFrame\n", |
464 | | - " '''\n", |
465 | | - " attr = 'background-color: {}'.format(color)\n", |
466 | | - " if data.ndim == 1: # Series from .apply(axis=0) or axis=1\n", |
467 | | - " is_max = data == data.max()\n", |
468 | | - " return [attr if v else '' for v in is_max]\n", |
469 | | - " else: # from .apply(axis=None)\n", |
470 | | - " is_max = data == data.max().max()\n", |
471 | | - " return pd.DataFrame(np.where(is_max, attr, ''),\n", |
472 | | - " index=data.index, columns=data.columns)" |
473 | | - ] |
474 | | - }, |
475 | | - { |
476 | | - "cell_type": "markdown", |
477 | | - "metadata": {}, |
478 | | - "source": [ |
479 | | - "When using ``Styler.apply(func, axis=None)``, the function must return a DataFrame with the same index and column labels." |
| 455 | + "Use `.apply(function, axis=None)` to indicate that your function wants the entire table, not one column or row at a time. In this case the return must be a DataFrame or ndarray of the same shape as the input. Let's try that next. " |
480 | 456 | ] |
481 | 457 | }, |
482 | 458 | { |
|
485 | 461 | "metadata": {}, |
486 | 462 | "outputs": [], |
487 | 463 | "source": [ |
488 | | - "s = df.style.apply(highlight_max, color='darkorange', axis=None)\n", |
| 464 | + "s = df.style.apply(highlight_max, axis=None)\n", |
489 | 465 | "s" |
490 | 466 | ] |
491 | 467 | }, |
|
0 commit comments