|
160 | 160 | "source": [ |
161 | 161 | "## Special Display Formats\n", |
162 | 162 | "\n", |
163 | | - "See [IPython example notebook](https://nbviewer.jupyter.org/github/ipython/ipython/blob/master/examples/IPython Kernel/Rich Output.ipynb).\n", |
164 | | - "\n", |
165 | | - "TODO: tables? e.g. Pandas DataFrame?" |
| 163 | + "See [IPython example notebook](https://nbviewer.jupyter.org/github/ipython/ipython/blob/master/examples/IPython Kernel/Rich Output.ipynb)." |
166 | 164 | ] |
167 | 165 | }, |
168 | 166 | { |
|
402 | 400 | "fig" |
403 | 401 | ] |
404 | 402 | }, |
| 403 | + { |
| 404 | + "cell_type": "markdown", |
| 405 | + "metadata": {}, |
| 406 | + "source": [ |
| 407 | + "### Pandas Dataframes\n", |
| 408 | + "\n", |
| 409 | + "[Pandas dataframes](http://pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe)\n", |
| 410 | + "should be displayed as nicely formatted HTML tables (if you are using HTML output)." |
| 411 | + ] |
| 412 | + }, |
| 413 | + { |
| 414 | + "cell_type": "code", |
| 415 | + "execution_count": null, |
| 416 | + "metadata": {}, |
| 417 | + "outputs": [], |
| 418 | + "source": [ |
| 419 | + "import numpy as np\n", |
| 420 | + "import pandas as pd" |
| 421 | + ] |
| 422 | + }, |
| 423 | + { |
| 424 | + "cell_type": "code", |
| 425 | + "execution_count": null, |
| 426 | + "metadata": {}, |
| 427 | + "outputs": [], |
| 428 | + "source": [ |
| 429 | + "df = pd.DataFrame(np.random.randint(0, 100, size=[5, 4]),\n", |
| 430 | + " columns=['a', 'b', 'c', 'd'])\n", |
| 431 | + "df" |
| 432 | + ] |
| 433 | + }, |
| 434 | + { |
| 435 | + "cell_type": "markdown", |
| 436 | + "metadata": {}, |
| 437 | + "source": [ |
| 438 | + "For LaTeX output, however, the plain text output is used by default.\n", |
| 439 | + "\n", |
| 440 | + "To get nice LaTeX tables, a few settings have to be changed:" |
| 441 | + ] |
| 442 | + }, |
| 443 | + { |
| 444 | + "cell_type": "code", |
| 445 | + "execution_count": null, |
| 446 | + "metadata": {}, |
| 447 | + "outputs": [], |
| 448 | + "source": [ |
| 449 | + "pd.set_option('display.latex.repr', True)" |
| 450 | + ] |
| 451 | + }, |
| 452 | + { |
| 453 | + "cell_type": "markdown", |
| 454 | + "metadata": {}, |
| 455 | + "source": [ |
| 456 | + "This is not enabled by default because of\n", |
| 457 | + "[Pandas issue #12182](https://github.com/pandas-dev/pandas/issues/12182).\n", |
| 458 | + "\n", |
| 459 | + "The generated LaTeX tables utilize the `booktabs` package, so you have to make sure that package is [loaded in the preamble](http://www.sphinx-doc.org/en/master/latex.html) with:\n", |
| 460 | + "\n", |
| 461 | + " \\usepackage{booktabs}\n", |
| 462 | + "\n", |
| 463 | + "In order to allow page breaks within tables, you should use:" |
| 464 | + ] |
| 465 | + }, |
| 466 | + { |
| 467 | + "cell_type": "code", |
| 468 | + "execution_count": null, |
| 469 | + "metadata": {}, |
| 470 | + "outputs": [], |
| 471 | + "source": [ |
| 472 | + "pd.set_option('display.latex.longtable', True)" |
| 473 | + ] |
| 474 | + }, |
| 475 | + { |
| 476 | + "cell_type": "markdown", |
| 477 | + "metadata": {}, |
| 478 | + "source": [ |
| 479 | + "The `longtable` package is already used by Sphinx,\n", |
| 480 | + "so you don't have to manually load it in the preamble.\n", |
| 481 | + "\n", |
| 482 | + "Finally, if you want to use LaTeX math expressions in your dataframe, you'll have to disable escaping:" |
| 483 | + ] |
| 484 | + }, |
| 485 | + { |
| 486 | + "cell_type": "code", |
| 487 | + "execution_count": null, |
| 488 | + "metadata": {}, |
| 489 | + "outputs": [], |
| 490 | + "source": [ |
| 491 | + "pd.set_option('display.latex.escape', False)" |
| 492 | + ] |
| 493 | + }, |
| 494 | + { |
| 495 | + "cell_type": "markdown", |
| 496 | + "metadata": {}, |
| 497 | + "source": [ |
| 498 | + "The above settings should have no influence on the HTML output, but the LaTeX output should now look nicer:" |
| 499 | + ] |
| 500 | + }, |
| 501 | + { |
| 502 | + "cell_type": "code", |
| 503 | + "execution_count": null, |
| 504 | + "metadata": {}, |
| 505 | + "outputs": [], |
| 506 | + "source": [ |
| 507 | + "df = pd.DataFrame(np.random.randint(0, 100, size=[10, 4]),\n", |
| 508 | + " columns=[r'$\\alpha$', r'$\\beta$', r'$\\gamma$', r'$\\delta$'])\n", |
| 509 | + "df" |
| 510 | + ] |
| 511 | + }, |
405 | 512 | { |
406 | 513 | "cell_type": "markdown", |
407 | 514 | "metadata": {}, |
|
0 commit comments