OSGeoLive-Notebooks/Pandas/solved - 03b - Some more ad...

1131 lines
33 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Advanced indexing"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"\n",
"import pandas as pd\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"try:\n",
" import seaborn\n",
"except ImportError:\n",
" pass\n",
"\n",
"pd.options.display.max_rows = 10"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This dataset is borrowed from the [PyCon tutorial of Brandon Rhodes](https://github.com/brandon-rhodes/pycon-pandas-tutorial/) (so all credit to him!). You can download these data from here: [`titles.csv`](https://drive.google.com/file/d/0B3G70MlBnCgKa0U4WFdWdGdVOFU/view?usp=sharing) and [`cast.csv`](https://drive.google.com/file/d/0B3G70MlBnCgKRzRmTWdQTUdjNnM/view?usp=sharing) and put them in the `/data` folder."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>title</th>\n",
" <th>year</th>\n",
" <th>name</th>\n",
" <th>type</th>\n",
" <th>character</th>\n",
" <th>n</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Suuri illusioni</td>\n",
" <td>1985</td>\n",
" <td>Homo $</td>\n",
" <td>actor</td>\n",
" <td>Guests</td>\n",
" <td>22</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Gangsta Rap: The Glockumentary</td>\n",
" <td>2007</td>\n",
" <td>Too $hort</td>\n",
" <td>actor</td>\n",
" <td>Himself</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Menace II Society</td>\n",
" <td>1993</td>\n",
" <td>Too $hort</td>\n",
" <td>actor</td>\n",
" <td>Lew-Loc</td>\n",
" <td>27</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Porndogs: The Adventures of Sadie</td>\n",
" <td>2009</td>\n",
" <td>Too $hort</td>\n",
" <td>actor</td>\n",
" <td>Bosco</td>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Stop Pepper Palmer</td>\n",
" <td>2014</td>\n",
" <td>Too $hort</td>\n",
" <td>actor</td>\n",
" <td>Himself</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" title year name type character n\n",
"0 Suuri illusioni 1985 Homo $ actor Guests 22\n",
"1 Gangsta Rap: The Glockumentary 2007 Too $hort actor Himself NaN\n",
"2 Menace II Society 1993 Too $hort actor Lew-Loc 27\n",
"3 Porndogs: The Adventures of Sadie 2009 Too $hort actor Bosco 3\n",
"4 Stop Pepper Palmer 2014 Too $hort actor Himself NaN"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cast = pd.read_csv('data/cast.csv')\n",
"cast.head()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>title</th>\n",
" <th>year</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>The Rising Son</td>\n",
" <td>1990</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Ashes of Kukulcan</td>\n",
" <td>2016</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>The Thousand Plane Raid</td>\n",
" <td>1969</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Crucea de piatra</td>\n",
" <td>1993</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>The 86</td>\n",
" <td>2015</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" title year\n",
"0 The Rising Son 1990\n",
"1 Ashes of Kukulcan 2016\n",
"2 The Thousand Plane Raid 1969\n",
"3 Crucea de piatra 1993\n",
"4 The 86 2015"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"titles = pd.read_csv('data/titles.csv')\n",
"titles.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setting columns as the index"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Why is it useful to have an index?\n",
"\n",
"- Giving meaningful labels to your data -> easier to remember which data are where\n",
"- Unleash some powerful methods, eg with a DatetimeIndex for time series\n",
"- Easier and faster selection of data\n",
"\n",
"It is this last one we are going to explore here!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Setting the `title` column as the index:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"c = cast.set_index('title')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>year</th>\n",
" <th>name</th>\n",
" <th>type</th>\n",
" <th>character</th>\n",
" <th>n</th>\n",
" </tr>\n",
" <tr>\n",
" <th>title</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>Suuri illusioni</th>\n",
" <td>1985</td>\n",
" <td>Homo $</td>\n",
" <td>actor</td>\n",
" <td>Guests</td>\n",
" <td>22</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Gangsta Rap: The Glockumentary</th>\n",
" <td>2007</td>\n",
" <td>Too $hort</td>\n",
" <td>actor</td>\n",
" <td>Himself</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Menace II Society</th>\n",
" <td>1993</td>\n",
" <td>Too $hort</td>\n",
" <td>actor</td>\n",
" <td>Lew-Loc</td>\n",
" <td>27</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Porndogs: The Adventures of Sadie</th>\n",
" <td>2009</td>\n",
" <td>Too $hort</td>\n",
" <td>actor</td>\n",
" <td>Bosco</td>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Stop Pepper Palmer</th>\n",
" <td>2014</td>\n",
" <td>Too $hort</td>\n",
" <td>actor</td>\n",
" <td>Himself</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" year name type character n\n",
"title \n",
"Suuri illusioni 1985 Homo $ actor Guests 22\n",
"Gangsta Rap: The Glockumentary 2007 Too $hort actor Himself NaN\n",
"Menace II Society 1993 Too $hort actor Lew-Loc 27\n",
"Porndogs: The Adventures of Sadie 2009 Too $hort actor Bosco 3\n",
"Stop Pepper Palmer 2014 Too $hort actor Himself NaN"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Instead of doing:"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 476 ms, sys: 16 ms, total: 492 ms\n",
"Wall time: 495 ms\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>title</th>\n",
" <th>year</th>\n",
" <th>name</th>\n",
" <th>type</th>\n",
" <th>character</th>\n",
" <th>n</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>11638</th>\n",
" <td>Macbeth</td>\n",
" <td>2015</td>\n",
" <td>Darren Adamson</td>\n",
" <td>actor</td>\n",
" <td>Soldier</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20153</th>\n",
" <td>Macbeth</td>\n",
" <td>1916</td>\n",
" <td>Spottiswoode Aitken</td>\n",
" <td>actor</td>\n",
" <td>Duncan</td>\n",
" <td>4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>23106</th>\n",
" <td>Macbeth</td>\n",
" <td>1948</td>\n",
" <td>Robert Alan</td>\n",
" <td>actor</td>\n",
" <td>Third Murderer</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>24080</th>\n",
" <td>Macbeth</td>\n",
" <td>2016</td>\n",
" <td>John Albasiny</td>\n",
" <td>actor</td>\n",
" <td>Doctor</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>34024</th>\n",
" <td>Macbeth</td>\n",
" <td>1948</td>\n",
" <td>William Alland</td>\n",
" <td>actor</td>\n",
" <td>Second Murderer</td>\n",
" <td>18</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3288130</th>\n",
" <td>Macbeth</td>\n",
" <td>1998</td>\n",
" <td>Jessica Werbin</td>\n",
" <td>actress</td>\n",
" <td>Lady Macduff</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3298214</th>\n",
" <td>Macbeth</td>\n",
" <td>2014</td>\n",
" <td>Finty Williams</td>\n",
" <td>actress</td>\n",
" <td>Lady Macduff</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3301599</th>\n",
" <td>Macbeth</td>\n",
" <td>2006</td>\n",
" <td>Jamie-Lee Wilson</td>\n",
" <td>actress</td>\n",
" <td>Female Constable</td>\n",
" <td>39</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3302941</th>\n",
" <td>Macbeth</td>\n",
" <td>1998</td>\n",
" <td>Dawn Winarski</td>\n",
" <td>actress</td>\n",
" <td>Lady Macbeth</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3311842</th>\n",
" <td>Macbeth</td>\n",
" <td>2006</td>\n",
" <td>Edwina Wren</td>\n",
" <td>actress</td>\n",
" <td>Malcolm's Girl</td>\n",
" <td>35</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>329 rows × 6 columns</p>\n",
"</div>"
],
"text/plain": [
" title year name type character n\n",
"11638 Macbeth 2015 Darren Adamson actor Soldier NaN\n",
"20153 Macbeth 1916 Spottiswoode Aitken actor Duncan 4\n",
"23106 Macbeth 1948 Robert Alan actor Third Murderer NaN\n",
"24080 Macbeth 2016 John Albasiny actor Doctor NaN\n",
"34024 Macbeth 1948 William Alland actor Second Murderer 18\n",
"... ... ... ... ... ... ..\n",
"3288130 Macbeth 1998 Jessica Werbin actress Lady Macduff NaN\n",
"3298214 Macbeth 2014 Finty Williams actress Lady Macduff NaN\n",
"3301599 Macbeth 2006 Jamie-Lee Wilson actress Female Constable 39\n",
"3302941 Macbeth 1998 Dawn Winarski actress Lady Macbeth 2\n",
"3311842 Macbeth 2006 Edwina Wren actress Malcolm's Girl 35\n",
"\n",
"[329 rows x 6 columns]"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%time\n",
"cast[cast['title'] == 'Hamlet']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"we can now do:"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 188 ms, sys: 4 ms, total: 192 ms\n",
"Wall time: 195 ms\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>year</th>\n",
" <th>name</th>\n",
" <th>type</th>\n",
" <th>character</th>\n",
" <th>n</th>\n",
" </tr>\n",
" <tr>\n",
" <th>title</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>Macbeth</th>\n",
" <td>2015</td>\n",
" <td>Darren Adamson</td>\n",
" <td>actor</td>\n",
" <td>Soldier</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Macbeth</th>\n",
" <td>1916</td>\n",
" <td>Spottiswoode Aitken</td>\n",
" <td>actor</td>\n",
" <td>Duncan</td>\n",
" <td>4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Macbeth</th>\n",
" <td>1948</td>\n",
" <td>Robert Alan</td>\n",
" <td>actor</td>\n",
" <td>Third Murderer</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Macbeth</th>\n",
" <td>2016</td>\n",
" <td>John Albasiny</td>\n",
" <td>actor</td>\n",
" <td>Doctor</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Macbeth</th>\n",
" <td>1948</td>\n",
" <td>William Alland</td>\n",
" <td>actor</td>\n",
" <td>Second Murderer</td>\n",
" <td>18</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Macbeth</th>\n",
" <td>1998</td>\n",
" <td>Jessica Werbin</td>\n",
" <td>actress</td>\n",
" <td>Lady Macduff</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Macbeth</th>\n",
" <td>2014</td>\n",
" <td>Finty Williams</td>\n",
" <td>actress</td>\n",
" <td>Lady Macduff</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Macbeth</th>\n",
" <td>2006</td>\n",
" <td>Jamie-Lee Wilson</td>\n",
" <td>actress</td>\n",
" <td>Female Constable</td>\n",
" <td>39</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Macbeth</th>\n",
" <td>1998</td>\n",
" <td>Dawn Winarski</td>\n",
" <td>actress</td>\n",
" <td>Lady Macbeth</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Macbeth</th>\n",
" <td>2006</td>\n",
" <td>Edwina Wren</td>\n",
" <td>actress</td>\n",
" <td>Malcolm's Girl</td>\n",
" <td>35</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>329 rows × 5 columns</p>\n",
"</div>"
],
"text/plain": [
" year name type character n\n",
"title \n",
"Macbeth 2015 Darren Adamson actor Soldier NaN\n",
"Macbeth 1916 Spottiswoode Aitken actor Duncan 4\n",
"Macbeth 1948 Robert Alan actor Third Murderer NaN\n",
"Macbeth 2016 John Albasiny actor Doctor NaN\n",
"Macbeth 1948 William Alland actor Second Murderer 18\n",
"... ... ... ... ... ..\n",
"Macbeth 1998 Jessica Werbin actress Lady Macduff NaN\n",
"Macbeth 2014 Finty Williams actress Lady Macduff NaN\n",
"Macbeth 2006 Jamie-Lee Wilson actress Female Constable 39\n",
"Macbeth 1998 Dawn Winarski actress Lady Macbeth 2\n",
"Macbeth 2006 Edwina Wren actress Malcolm's Girl 35\n",
"\n",
"[329 rows x 5 columns]"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%time\n",
"c.loc['Hamlet']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"But you can also have multiple columns as the index, leading to a **multi-index or hierarchical index**:"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"c = cast.set_index(['title', 'year'])"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th></th>\n",
" <th>name</th>\n",
" <th>type</th>\n",
" <th>character</th>\n",
" <th>n</th>\n",
" </tr>\n",
" <tr>\n",
" <th>title</th>\n",
" <th>year</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>Suuri illusioni</th>\n",
" <th>1985</th>\n",
" <td>Homo $</td>\n",
" <td>actor</td>\n",
" <td>Guests</td>\n",
" <td>22</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Gangsta Rap: The Glockumentary</th>\n",
" <th>2007</th>\n",
" <td>Too $hort</td>\n",
" <td>actor</td>\n",
" <td>Himself</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Menace II Society</th>\n",
" <th>1993</th>\n",
" <td>Too $hort</td>\n",
" <td>actor</td>\n",
" <td>Lew-Loc</td>\n",
" <td>27</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Porndogs: The Adventures of Sadie</th>\n",
" <th>2009</th>\n",
" <td>Too $hort</td>\n",
" <td>actor</td>\n",
" <td>Bosco</td>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Stop Pepper Palmer</th>\n",
" <th>2014</th>\n",
" <td>Too $hort</td>\n",
" <td>actor</td>\n",
" <td>Himself</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" name type character n\n",
"title year \n",
"Suuri illusioni 1985 Homo $ actor Guests 22\n",
"Gangsta Rap: The Glockumentary 2007 Too $hort actor Himself NaN\n",
"Menace II Society 1993 Too $hort actor Lew-Loc 27\n",
"Porndogs: The Adventures of Sadie 2009 Too $hort actor Bosco 3\n",
"Stop Pepper Palmer 2014 Too $hort actor Himself NaN"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c.head()"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 40 ms, sys: 12 ms, total: 52 ms\n",
"Wall time: 50.5 ms\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/joris/miniconda/envs/python3/lib/python3.4/site-packages/pandas/core/index.py:5091: PerformanceWarning: indexing past lexsort depth may impact performance.\n",
" PerformanceWarning)\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th></th>\n",
" <th>name</th>\n",
" <th>type</th>\n",
" <th>character</th>\n",
" <th>n</th>\n",
" </tr>\n",
" <tr>\n",
" <th>title</th>\n",
" <th>year</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th rowspan=\"11\" valign=\"top\">Hamlet</th>\n",
" <th>2000</th>\n",
" <td>Casey Affleck</td>\n",
" <td>actor</td>\n",
" <td>Fortinbras</td>\n",
" <td>15</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2000</th>\n",
" <td>Paul Bartel</td>\n",
" <td>actor</td>\n",
" <td>Osric</td>\n",
" <td>14</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2000</th>\n",
" <td>Paul Ferriter</td>\n",
" <td>actor</td>\n",
" <td>Special Guest Appearance</td>\n",
" <td>23</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2000</th>\n",
" <td>Larry Fessenden</td>\n",
" <td>actor</td>\n",
" <td>Kissing Man</td>\n",
" <td>24</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2000</th>\n",
" <td>Karl Geary</td>\n",
" <td>actor</td>\n",
" <td>Horatio</td>\n",
" <td>8</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2000</th>\n",
" <td>Anne (II) Nixon</td>\n",
" <td>actress</td>\n",
" <td>Special Guest Appearance</td>\n",
" <td>34</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2000</th>\n",
" <td>India Reed Kotis</td>\n",
" <td>actress</td>\n",
" <td>Special Guest Appearance</td>\n",
" <td>29</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2000</th>\n",
" <td>Kelly Sebastian</td>\n",
" <td>actress</td>\n",
" <td>Secretary</td>\n",
" <td>39</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2000</th>\n",
" <td>Julia Stiles</td>\n",
" <td>actress</td>\n",
" <td>Ophelia</td>\n",
" <td>7</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2000</th>\n",
" <td>Diane Venora</td>\n",
" <td>actress</td>\n",
" <td>Gertrude</td>\n",
" <td>3</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>38 rows × 4 columns</p>\n",
"</div>"
],
"text/plain": [
" name type character n\n",
"title year \n",
"Hamlet 2000 Casey Affleck actor Fortinbras 15\n",
" 2000 Paul Bartel actor Osric 14\n",
" 2000 Paul Ferriter actor Special Guest Appearance 23\n",
" 2000 Larry Fessenden actor Kissing Man 24\n",
" 2000 Karl Geary actor Horatio 8\n",
"... ... ... ... ..\n",
" 2000 Anne (II) Nixon actress Special Guest Appearance 34\n",
" 2000 India Reed Kotis actress Special Guest Appearance 29\n",
" 2000 Kelly Sebastian actress Secretary 39\n",
" 2000 Julia Stiles actress Ophelia 7\n",
" 2000 Diane Venora actress Gertrude 3\n",
"\n",
"[38 rows x 4 columns]"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%time\n",
"c.loc[('Hamlet', 2000),:]"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"c2 = c.sort_index()"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 8 ms, sys: 0 ns, total: 8 ms\n",
"Wall time: 7.3 ms\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th></th>\n",
" <th>name</th>\n",
" <th>type</th>\n",
" <th>character</th>\n",
" <th>n</th>\n",
" </tr>\n",
" <tr>\n",
" <th>title</th>\n",
" <th>year</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th rowspan=\"11\" valign=\"top\">Hamlet</th>\n",
" <th>2000</th>\n",
" <td>Casey Affleck</td>\n",
" <td>actor</td>\n",
" <td>Fortinbras</td>\n",
" <td>15</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2000</th>\n",
" <td>Paul Bartel</td>\n",
" <td>actor</td>\n",
" <td>Osric</td>\n",
" <td>14</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2000</th>\n",
" <td>Paul Ferriter</td>\n",
" <td>actor</td>\n",
" <td>Special Guest Appearance</td>\n",
" <td>23</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2000</th>\n",
" <td>Larry Fessenden</td>\n",
" <td>actor</td>\n",
" <td>Kissing Man</td>\n",
" <td>24</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2000</th>\n",
" <td>Karl Geary</td>\n",
" <td>actor</td>\n",
" <td>Horatio</td>\n",
" <td>8</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2000</th>\n",
" <td>Anne (II) Nixon</td>\n",
" <td>actress</td>\n",
" <td>Special Guest Appearance</td>\n",
" <td>34</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2000</th>\n",
" <td>India Reed Kotis</td>\n",
" <td>actress</td>\n",
" <td>Special Guest Appearance</td>\n",
" <td>29</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2000</th>\n",
" <td>Kelly Sebastian</td>\n",
" <td>actress</td>\n",
" <td>Secretary</td>\n",
" <td>39</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2000</th>\n",
" <td>Julia Stiles</td>\n",
" <td>actress</td>\n",
" <td>Ophelia</td>\n",
" <td>7</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2000</th>\n",
" <td>Diane Venora</td>\n",
" <td>actress</td>\n",
" <td>Gertrude</td>\n",
" <td>3</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>38 rows × 4 columns</p>\n",
"</div>"
],
"text/plain": [
" name type character n\n",
"title year \n",
"Hamlet 2000 Casey Affleck actor Fortinbras 15\n",
" 2000 Paul Bartel actor Osric 14\n",
" 2000 Paul Ferriter actor Special Guest Appearance 23\n",
" 2000 Larry Fessenden actor Kissing Man 24\n",
" 2000 Karl Geary actor Horatio 8\n",
"... ... ... ... ..\n",
" 2000 Anne (II) Nixon actress Special Guest Appearance 34\n",
" 2000 India Reed Kotis actress Special Guest Appearance 29\n",
" 2000 Kelly Sebastian actress Secretary 39\n",
" 2000 Julia Stiles actress Ophelia 7\n",
" 2000 Diane Venora actress Gertrude 3\n",
"\n",
"[38 rows x 4 columns]"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%time\n",
"c2.loc[('Hamlet', 2000),:]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"celltoolbar": "Nbtutor - export exercises",
"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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}