{
"metadata": {
"name": "",
"signature": "sha256:ca0f00132de7ae9c8ef76dfc7f5c8051f310d01f010677297806ee2bd2cf79e0"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# itable examples\n",
"\n",
"These tables are created using the itable package [https://github.com/mgymrek/itable](https://github.com/mgymrek/itable).\n",
"\n",
"These examples are originally described in [this post](http://melissagymrek.com/python/2014/01/12/ipython-tables.html) before I had created the separate ```itable``` package. Usage is identical except for the change in the package name."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import pandas as pd\n",
"import itable"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 16
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Create an example data frame\n",
"df = pd.DataFrame({\"x\":[1,2,3], \"y\":[6,4,3], \"z\":[\"testing\",\"pretty\",\"tables\"], \"f\":[0.023432, 0.234321,0.5555]})"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 17
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Display a simple table\n",
"itable.PrettyTable(df)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"
| f | x | y | z |
| 0.023432 | 1 | 6 | testing |
| 0.234321 | 2 | 4 | pretty |
| 0.5555 | 3 | 3 | tables |
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 18,
"text": [
""
]
}
],
"prompt_number": 18
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Give it a theme and center the table\n",
"itable.PrettyTable(df, tstyle=itable.TableStyle(theme=\"theme1\"), center=True)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"| f | x | y | z |
| 0.023432 | 1 | 6 | testing |
| 0.234321 | 2 | 4 | pretty |
| 0.5555 | 3 | 3 | tables |
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 19,
"text": [
""
]
}
],
"prompt_number": 19
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Set cell style using a CellStyle object\n",
"pt = itable.PrettyTable(df, tstyle=itable.TableStyle(theme=\"theme1\"), center=True)\n",
"cs = itable.CellStyle()\n",
"cs.set(\"background-color\", \"red\")\n",
"cs.set(\"color\", \"white\")\n",
"pt.set_cell_style(style=cs)\n",
"pt"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"| f | x | y | z |
| 0.023432 | 1 | 6 | testing |
| 0.234321 | 2 | 4 | pretty |
| 0.5555 | 3 | 3 | tables |
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 20,
"text": [
""
]
}
],
"prompt_number": 20
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Only for a subset of rows/columns\n",
"pt = itable.PrettyTable(df, tstyle=itable.TableStyle(theme=\"theme1\"), center=True)\n",
"cs = itable.CellStyle()\n",
"cs.set(\"background-color\", \"red\")\n",
"cs.set(\"color\", \"white\")\n",
"pt.set_cell_style(style=cs, rows=[1,2], cols=[2])\n",
"pt"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"| f | x | y | z |
| 0.023432 | 1 | 6 | testing |
| 0.234321 | 2 | 4 | pretty |
| 0.5555 | 3 | 3 | tables |
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 21,
"text": [
""
]
}
],
"prompt_number": 21
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Set styles using keywords (these are all CSS properties, just replace \"-\" with \"_\")\n",
"pt = itable.PrettyTable(df, tstyle=itable.TableStyle(theme=\"theme1\"), center=True)\n",
"pt.set_cell_style(font_weight=\"bold\", color=\"purple\", background_color=\"yellow\", rows=[1], cols=[2,3])\n",
"pt"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"| f | x | y | z |
| 0.023432 | 1 | 6 | testing |
| 0.234321 | 2 | 4 | pretty |
| 0.5555 | 3 | 3 | tables |
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 22,
"text": [
""
]
}
],
"prompt_number": 22
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Set the header style. corner sets the corner cell\n",
"# \"indices\" applies the style only to certain indices\n",
"pt = itable.PrettyTable(df, tstyle=itable.TableStyle(theme=\"theme1\"), center=True, header_row=True)\n",
"pt.set_row_header_style(color=\"blue\")\n",
"pt.set_col_header_style(background_color=\"white\", font_weight=\"bold\", indices=[2])\n",
"pt.set_corner_style(background_color=\"red\")\n",
"pt"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
" | f | x | y | z |
| 0 | 0.023432 | 1 | 6 | testing |
| 1 | 0.234321 | 2 | 4 | pretty |
| 2 | 0.5555 | 3 | 3 | tables |
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 23,
"text": [
""
]
}
],
"prompt_number": 23
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Update current styles\n",
"pt = itable.PrettyTable(df, tstyle=itable.TableStyle(theme=\"theme1\"), center=True, header_row=True)\n",
"pt.set_cell_style(font_weight=\"bold\", color=\"purple\", background_color=\"yellow\", rows=[1], cols=[2,3])\n",
"pt.update_cell_style(background_color=\"pink\", rows=[1], cols=[3])\n",
"pt.update_row_header_style(background_color=\"blue\", indices=[0])\n",
"pt.update_col_header_style(color=\"red\")\n",
"pt.update_corner_style(background_color=\"purple\")\n",
"pt"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
" | f | x | y | z |
| 0 | 0.023432 | 1 | 6 | testing |
| 1 | 0.234321 | 2 | 4 | pretty |
| 2 | 0.5555 | 3 | 3 | tables |
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 24,
"text": [
""
]
}
],
"prompt_number": 24
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Set cell formats\n",
"df = pd.DataFrame({\"FloatColumn\": [0.0324234, 0.23432111, 0.555555], \"SciNotColumn\":[1e10, 4.232e-6, 53e-8]})\n",
"pt = itable.PrettyTable(df, tstyle=itable.TableStyle(theme=\"theme1\"), center=True)\n",
"pt.set_cell_style(cols=[0], format_function=lambda x: \"%.3f\"%x)\n",
"def SciNot(x):\n",
" xx = \"%.2E\"%x\n",
" base, exp = xx.split(\"E\")\n",
" return \"%s × 10%s\"%(base, int(exp))\n",
"pt.set_cell_style(cols=[1], format_function=SciNot)\n",
"pt"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"| FloatColumn | SciNotColumn |
| 0.032 | 1.00 × 1010 |
| 0.234 | 4.23 × 10-6 |
| 0.556 | 5.30 × 10-7 |
"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 25,
"text": [
""
]
}
],
"prompt_number": 25
}
],
"metadata": {}
}
]
}