data-science-ipython-notebooks/pandas/03.05-Hierarchical-Indexing.ipynb
2017-03-13 04:42:29 -04:00

2521 lines
69 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!--BOOK_INFORMATION-->\n",
"<img align=\"left\" style=\"padding-right:10px;\" src=\"figures/PDSH-cover-small.png\">\n",
"*This notebook contains an excerpt from the [Python Data Science Handbook](http://shop.oreilly.com/product/0636920034919.do) by Jake VanderPlas; the content is available [on GitHub](https://github.com/jakevdp/PythonDataScienceHandbook).*\n",
"\n",
"*The text is released under the [CC-BY-NC-ND license](https://creativecommons.org/licenses/by-nc-nd/3.0/us/legalcode), and code is released under the [MIT license](https://opensource.org/licenses/MIT). If you find this content useful, please consider supporting the work by [buying the book](http://shop.oreilly.com/product/0636920034919.do)!*\n",
"\n",
"*No changes were made to the contents of this notebook from the original.*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!--NAVIGATION-->\n",
"< [Handling Missing Data](03.04-Missing-Values.ipynb) | [Contents](Index.ipynb) | [Combining Datasets: Concat and Append](03.06-Concat-And-Append.ipynb) >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Hierarchical Indexing\n",
"\n",
"Up to this point we've been focused primarily on one-dimensional and two-dimensional data, stored in Pandas ``Series`` and ``DataFrame`` objects, respectively.\n",
"Often it is useful to go beyond this and store higher-dimensional datathat is, data indexed by more than one or two keys.\n",
"While Pandas does provide ``Panel`` and ``Panel4D`` objects that natively handle three-dimensional and four-dimensional data (see [Aside: Panel Data](#Aside:-Panel-Data)), a far more common pattern in practice is to make use of *hierarchical indexing* (also known as *multi-indexing*) to incorporate multiple index *levels* within a single index.\n",
"In this way, higher-dimensional data can be compactly represented within the familiar one-dimensional ``Series`` and two-dimensional ``DataFrame`` objects.\n",
"\n",
"In this section, we'll explore the direct creation of ``MultiIndex`` objects, considerations when indexing, slicing, and computing statistics across multiply indexed data, and useful routines for converting between simple and hierarchically indexed representations of your data.\n",
"\n",
"We begin with the standard imports:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## A Multiply Indexed Series\n",
"\n",
"Let's start by considering how we might represent two-dimensional data within a one-dimensional ``Series``.\n",
"For concreteness, we will consider a series of data where each point has a character and numerical key."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### The bad way\n",
"\n",
"Suppose you would like to track data about states from two different years.\n",
"Using the Pandas tools we've already covered, you might be tempted to simply use Python tuples as keys:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(California, 2000) 33871648\n",
"(California, 2010) 37253956\n",
"(New York, 2000) 18976457\n",
"(New York, 2010) 19378102\n",
"(Texas, 2000) 20851820\n",
"(Texas, 2010) 25145561\n",
"dtype: int64"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"index = [('California', 2000), ('California', 2010),\n",
" ('New York', 2000), ('New York', 2010),\n",
" ('Texas', 2000), ('Texas', 2010)]\n",
"populations = [33871648, 37253956,\n",
" 18976457, 19378102,\n",
" 20851820, 25145561]\n",
"pop = pd.Series(populations, index=index)\n",
"pop"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"With this indexing scheme, you can straightforwardly index or slice the series based on this multiple index:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(California, 2010) 37253956\n",
"(New York, 2000) 18976457\n",
"(New York, 2010) 19378102\n",
"(Texas, 2000) 20851820\n",
"dtype: int64"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop[('California', 2010):('Texas', 2000)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"But the convenience ends there. For example, if you need to select all values from 2010, you'll need to do some messy (and potentially slow) munging to make it happen:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(California, 2010) 37253956\n",
"(New York, 2010) 19378102\n",
"(Texas, 2010) 25145561\n",
"dtype: int64"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop[[i for i in pop.index if i[1] == 2010]]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This produces the desired result, but is not as clean (or as efficient for large datasets) as the slicing syntax we've grown to love in Pandas."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### The Better Way: Pandas MultiIndex\n",
"Fortunately, Pandas provides a better way.\n",
"Our tuple-based indexing is essentially a rudimentary multi-index, and the Pandas ``MultiIndex`` type gives us the type of operations we wish to have.\n",
"We can create a multi-index from the tuples as follows:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"MultiIndex(levels=[['California', 'New York', 'Texas'], [2000, 2010]],\n",
" labels=[[0, 0, 1, 1, 2, 2], [0, 1, 0, 1, 0, 1]])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"index = pd.MultiIndex.from_tuples(index)\n",
"index"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice that the ``MultiIndex`` contains multiple *levels* of indexingin this case, the state names and the years, as well as multiple *labels* for each data point which encode these levels.\n",
"\n",
"If we re-index our series with this ``MultiIndex``, we see the hierarchical representation of the data:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"California 2000 33871648\n",
" 2010 37253956\n",
"New York 2000 18976457\n",
" 2010 19378102\n",
"Texas 2000 20851820\n",
" 2010 25145561\n",
"dtype: int64"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop = pop.reindex(index)\n",
"pop"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here the first two columns of the ``Series`` representation show the multiple index values, while the third column shows the data.\n",
"Notice that some entries are missing in the first column: in this multi-index representation, any blank entry indicates the same value as the line above it."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now to access all data for which the second index is 2010, we can simply use the Pandas slicing notation:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"California 37253956\n",
"New York 19378102\n",
"Texas 25145561\n",
"dtype: int64"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop[:, 2010]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The result is a singly indexed array with just the keys we're interested in.\n",
"This syntax is much more convenient (and the operation is much more efficient!) than the home-spun tuple-based multi-indexing solution that we started with.\n",
"We'll now further discuss this sort of indexing operation on hieararchically indexed data."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### MultiIndex as extra dimension\n",
"\n",
"You might notice something else here: we could easily have stored the same data using a simple ``DataFrame`` with index and column labels.\n",
"In fact, Pandas is built with this equivalence in mind. The ``unstack()`` method will quickly convert a multiply indexed ``Series`` into a conventionally indexed ``DataFrame``:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"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>2000</th>\n",
" <th>2010</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>California</th>\n",
" <td>33871648</td>\n",
" <td>37253956</td>\n",
" </tr>\n",
" <tr>\n",
" <th>New York</th>\n",
" <td>18976457</td>\n",
" <td>19378102</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Texas</th>\n",
" <td>20851820</td>\n",
" <td>25145561</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" 2000 2010\n",
"California 33871648 37253956\n",
"New York 18976457 19378102\n",
"Texas 20851820 25145561"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop_df = pop.unstack()\n",
"pop_df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Naturally, the ``stack()`` method provides the opposite operation:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"California 2000 33871648\n",
" 2010 37253956\n",
"New York 2000 18976457\n",
" 2010 19378102\n",
"Texas 2000 20851820\n",
" 2010 25145561\n",
"dtype: int64"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop_df.stack()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Seeing this, you might wonder why would we would bother with hierarchical indexing at all.\n",
"The reason is simple: just as we were able to use multi-indexing to represent two-dimensional data within a one-dimensional ``Series``, we can also use it to represent data of three or more dimensions in a ``Series`` or ``DataFrame``.\n",
"Each extra level in a multi-index represents an extra dimension of data; taking advantage of this property gives us much more flexibility in the types of data we can represent. Concretely, we might want to add another column of demographic data for each state at each year (say, population under 18) ; with a ``MultiIndex`` this is as easy as adding another column to the ``DataFrame``:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"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>total</th>\n",
" <th>under18</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">California</th>\n",
" <th>2000</th>\n",
" <td>33871648</td>\n",
" <td>9267089</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2010</th>\n",
" <td>37253956</td>\n",
" <td>9284094</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">New York</th>\n",
" <th>2000</th>\n",
" <td>18976457</td>\n",
" <td>4687374</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2010</th>\n",
" <td>19378102</td>\n",
" <td>4318033</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">Texas</th>\n",
" <th>2000</th>\n",
" <td>20851820</td>\n",
" <td>5906301</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2010</th>\n",
" <td>25145561</td>\n",
" <td>6879014</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" total under18\n",
"California 2000 33871648 9267089\n",
" 2010 37253956 9284094\n",
"New York 2000 18976457 4687374\n",
" 2010 19378102 4318033\n",
"Texas 2000 20851820 5906301\n",
" 2010 25145561 6879014"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop_df = pd.DataFrame({'total': pop,\n",
" 'under18': [9267089, 9284094,\n",
" 4687374, 4318033,\n",
" 5906301, 6879014]})\n",
"pop_df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In addition, all the ufuncs and other functionality discussed in [Operating on Data in Pandas](03.03-Operations-in-Pandas.ipynb) work with hierarchical indices as well.\n",
"Here we compute the fraction of people under 18 by year, given the above data:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"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>2000</th>\n",
" <th>2010</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>California</th>\n",
" <td>0.273594</td>\n",
" <td>0.249211</td>\n",
" </tr>\n",
" <tr>\n",
" <th>New York</th>\n",
" <td>0.247010</td>\n",
" <td>0.222831</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Texas</th>\n",
" <td>0.283251</td>\n",
" <td>0.273568</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" 2000 2010\n",
"California 0.273594 0.249211\n",
"New York 0.247010 0.222831\n",
"Texas 0.283251 0.273568"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f_u18 = pop_df['under18'] / pop_df['total']\n",
"f_u18.unstack()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This allows us to easily and quickly manipulate and explore even high-dimensional data."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Methods of MultiIndex Creation\n",
"\n",
"The most straightforward way to construct a multiply indexed ``Series`` or ``DataFrame`` is to simply pass a list of two or more index arrays to the constructor. For example:"
]
},
{
"cell_type": "code",
"execution_count": 12,
"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>data1</th>\n",
" <th>data2</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">a</th>\n",
" <th>1</th>\n",
" <td>0.554233</td>\n",
" <td>0.356072</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>0.925244</td>\n",
" <td>0.219474</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">b</th>\n",
" <th>1</th>\n",
" <td>0.441759</td>\n",
" <td>0.610054</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>0.171495</td>\n",
" <td>0.886688</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" data1 data2\n",
"a 1 0.554233 0.356072\n",
" 2 0.925244 0.219474\n",
"b 1 0.441759 0.610054\n",
" 2 0.171495 0.886688"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.DataFrame(np.random.rand(4, 2),\n",
" index=[['a', 'a', 'b', 'b'], [1, 2, 1, 2]],\n",
" columns=['data1', 'data2'])\n",
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The work of creating the ``MultiIndex`` is done in the background.\n",
"\n",
"Similarly, if you pass a dictionary with appropriate tuples as keys, Pandas will automatically recognize this and use a ``MultiIndex`` by default:"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"California 2000 33871648\n",
" 2010 37253956\n",
"New York 2000 18976457\n",
" 2010 19378102\n",
"Texas 2000 20851820\n",
" 2010 25145561\n",
"dtype: int64"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = {('California', 2000): 33871648,\n",
" ('California', 2010): 37253956,\n",
" ('Texas', 2000): 20851820,\n",
" ('Texas', 2010): 25145561,\n",
" ('New York', 2000): 18976457,\n",
" ('New York', 2010): 19378102}\n",
"pd.Series(data)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Nevertheless, it is sometimes useful to explicitly create a ``MultiIndex``; we'll see a couple of these methods here."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Explicit MultiIndex constructors\n",
"\n",
"For more flexibility in how the index is constructed, you can instead use the class method constructors available in the ``pd.MultiIndex``.\n",
"For example, as we did before, you can construct the ``MultiIndex`` from a simple list of arrays giving the index values within each level:"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"MultiIndex(levels=[['a', 'b'], [1, 2]],\n",
" labels=[[0, 0, 1, 1], [0, 1, 0, 1]])"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.MultiIndex.from_arrays([['a', 'a', 'b', 'b'], [1, 2, 1, 2]])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can construct it from a list of tuples giving the multiple index values of each point:"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"MultiIndex(levels=[['a', 'b'], [1, 2]],\n",
" labels=[[0, 0, 1, 1], [0, 1, 0, 1]])"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.MultiIndex.from_tuples([('a', 1), ('a', 2), ('b', 1), ('b', 2)])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can even construct it from a Cartesian product of single indices:"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"MultiIndex(levels=[['a', 'b'], [1, 2]],\n",
" labels=[[0, 0, 1, 1], [0, 1, 0, 1]])"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.MultiIndex.from_product([['a', 'b'], [1, 2]])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Similarly, you can construct the ``MultiIndex`` directly using its internal encoding by passing ``levels`` (a list of lists containing available index values for each level) and ``labels`` (a list of lists that reference these labels):"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"MultiIndex(levels=[['a', 'b'], [1, 2]],\n",
" labels=[[0, 0, 1, 1], [0, 1, 0, 1]])"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.MultiIndex(levels=[['a', 'b'], [1, 2]],\n",
" labels=[[0, 0, 1, 1], [0, 1, 0, 1]])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Any of these objects can be passed as the ``index`` argument when creating a ``Series`` or ``Dataframe``, or be passed to the ``reindex`` method of an existing ``Series`` or ``DataFrame``."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### MultiIndex level names\n",
"\n",
"Sometimes it is convenient to name the levels of the ``MultiIndex``.\n",
"This can be accomplished by passing the ``names`` argument to any of the above ``MultiIndex`` constructors, or by setting the ``names`` attribute of the index after the fact:"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"state year\n",
"California 2000 33871648\n",
" 2010 37253956\n",
"New York 2000 18976457\n",
" 2010 19378102\n",
"Texas 2000 20851820\n",
" 2010 25145561\n",
"dtype: int64"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop.index.names = ['state', 'year']\n",
"pop"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"With more involved datasets, this can be a useful way to keep track of the meaning of various index values."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### MultiIndex for columns\n",
"\n",
"In a ``DataFrame``, the rows and columns are completely symmetric, and just as the rows can have multiple levels of indices, the columns can have multiple levels as well.\n",
"Consider the following, which is a mock-up of some (somewhat realistic) medical data:"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr>\n",
" <th></th>\n",
" <th>subject</th>\n",
" <th colspan=\"2\" halign=\"left\">Bob</th>\n",
" <th colspan=\"2\" halign=\"left\">Guido</th>\n",
" <th colspan=\"2\" halign=\"left\">Sue</th>\n",
" </tr>\n",
" <tr>\n",
" <th></th>\n",
" <th>type</th>\n",
" <th>HR</th>\n",
" <th>Temp</th>\n",
" <th>HR</th>\n",
" <th>Temp</th>\n",
" <th>HR</th>\n",
" <th>Temp</th>\n",
" </tr>\n",
" <tr>\n",
" <th>year</th>\n",
" <th>visit</th>\n",
" <th></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 rowspan=\"2\" valign=\"top\">2013</th>\n",
" <th>1</th>\n",
" <td>31.0</td>\n",
" <td>38.7</td>\n",
" <td>32.0</td>\n",
" <td>36.7</td>\n",
" <td>35.0</td>\n",
" <td>37.2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>44.0</td>\n",
" <td>37.7</td>\n",
" <td>50.0</td>\n",
" <td>35.0</td>\n",
" <td>29.0</td>\n",
" <td>36.7</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">2014</th>\n",
" <th>1</th>\n",
" <td>30.0</td>\n",
" <td>37.4</td>\n",
" <td>39.0</td>\n",
" <td>37.8</td>\n",
" <td>61.0</td>\n",
" <td>36.9</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>47.0</td>\n",
" <td>37.8</td>\n",
" <td>48.0</td>\n",
" <td>37.3</td>\n",
" <td>51.0</td>\n",
" <td>36.5</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
"subject Bob Guido Sue \n",
"type HR Temp HR Temp HR Temp\n",
"year visit \n",
"2013 1 31.0 38.7 32.0 36.7 35.0 37.2\n",
" 2 44.0 37.7 50.0 35.0 29.0 36.7\n",
"2014 1 30.0 37.4 39.0 37.8 61.0 36.9\n",
" 2 47.0 37.8 48.0 37.3 51.0 36.5"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# hierarchical indices and columns\n",
"index = pd.MultiIndex.from_product([[2013, 2014], [1, 2]],\n",
" names=['year', 'visit'])\n",
"columns = pd.MultiIndex.from_product([['Bob', 'Guido', 'Sue'], ['HR', 'Temp']],\n",
" names=['subject', 'type'])\n",
"\n",
"# mock some data\n",
"data = np.round(np.random.randn(4, 6), 1)\n",
"data[:, ::2] *= 10\n",
"data += 37\n",
"\n",
"# create the DataFrame\n",
"health_data = pd.DataFrame(data, index=index, columns=columns)\n",
"health_data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here we see where the multi-indexing for both rows and columns can come in *very* handy.\n",
"This is fundamentally four-dimensional data, where the dimensions are the subject, the measurement type, the year, and the visit number.\n",
"With this in place we can, for example, index the top-level column by the person's name and get a full ``DataFrame`` containing just that person's information:"
]
},
{
"cell_type": "code",
"execution_count": 20,
"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>type</th>\n",
" <th>HR</th>\n",
" <th>Temp</th>\n",
" </tr>\n",
" <tr>\n",
" <th>year</th>\n",
" <th>visit</th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">2013</th>\n",
" <th>1</th>\n",
" <td>32.0</td>\n",
" <td>36.7</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>50.0</td>\n",
" <td>35.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">2014</th>\n",
" <th>1</th>\n",
" <td>39.0</td>\n",
" <td>37.8</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>48.0</td>\n",
" <td>37.3</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
"type HR Temp\n",
"year visit \n",
"2013 1 32.0 36.7\n",
" 2 50.0 35.0\n",
"2014 1 39.0 37.8\n",
" 2 48.0 37.3"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"health_data['Guido']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For complicated records containing multiple labeled measurements across multiple times for many subjects (people, countries, cities, etc.) use of hierarchical rows and columns can be extremely convenient!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Indexing and Slicing a MultiIndex\n",
"\n",
"Indexing and slicing on a ``MultiIndex`` is designed to be intuitive, and it helps if you think about the indices as added dimensions.\n",
"We'll first look at indexing multiply indexed ``Series``, and then multiply-indexed ``DataFrame``s."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Multiply indexed Series\n",
"\n",
"Consider the multiply indexed ``Series`` of state populations we saw earlier:"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"state year\n",
"California 2000 33871648\n",
" 2010 37253956\n",
"New York 2000 18976457\n",
" 2010 19378102\n",
"Texas 2000 20851820\n",
" 2010 25145561\n",
"dtype: int64"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can access single elements by indexing with multiple terms:"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"33871648"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop['California', 2000]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The ``MultiIndex`` also supports *partial indexing*, or indexing just one of the levels in the index.\n",
"The result is another ``Series``, with the lower-level indices maintained:"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"year\n",
"2000 33871648\n",
"2010 37253956\n",
"dtype: int64"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop['California']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Partial slicing is available as well, as long as the ``MultiIndex`` is sorted (see discussion in [Sorted and Unsorted Indices](Sorted-and-Unsorted-Indices)):"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"state year\n",
"California 2000 33871648\n",
" 2010 37253956\n",
"New York 2000 18976457\n",
" 2010 19378102\n",
"dtype: int64"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop.loc['California':'New York']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"With sorted indices, partial indexing can be performed on lower levels by passing an empty slice in the first index:"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"state\n",
"California 33871648\n",
"New York 18976457\n",
"Texas 20851820\n",
"dtype: int64"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop[:, 2000]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Other types of indexing and selection (discussed in [Data Indexing and Selection](03.02-Data-Indexing-and-Selection.ipynb)) work as well; for example, selection based on Boolean masks:"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"state year\n",
"California 2000 33871648\n",
" 2010 37253956\n",
"Texas 2010 25145561\n",
"dtype: int64"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop[pop > 22000000]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Selection based on fancy indexing also works:"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"state year\n",
"California 2000 33871648\n",
" 2010 37253956\n",
"Texas 2000 20851820\n",
" 2010 25145561\n",
"dtype: int64"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop[['California', 'Texas']]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Multiply indexed DataFrames\n",
"\n",
"A multiply indexed ``DataFrame`` behaves in a similar manner.\n",
"Consider our toy medical ``DataFrame`` from before:"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr>\n",
" <th></th>\n",
" <th>subject</th>\n",
" <th colspan=\"2\" halign=\"left\">Bob</th>\n",
" <th colspan=\"2\" halign=\"left\">Guido</th>\n",
" <th colspan=\"2\" halign=\"left\">Sue</th>\n",
" </tr>\n",
" <tr>\n",
" <th></th>\n",
" <th>type</th>\n",
" <th>HR</th>\n",
" <th>Temp</th>\n",
" <th>HR</th>\n",
" <th>Temp</th>\n",
" <th>HR</th>\n",
" <th>Temp</th>\n",
" </tr>\n",
" <tr>\n",
" <th>year</th>\n",
" <th>visit</th>\n",
" <th></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 rowspan=\"2\" valign=\"top\">2013</th>\n",
" <th>1</th>\n",
" <td>31.0</td>\n",
" <td>38.7</td>\n",
" <td>32.0</td>\n",
" <td>36.7</td>\n",
" <td>35.0</td>\n",
" <td>37.2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>44.0</td>\n",
" <td>37.7</td>\n",
" <td>50.0</td>\n",
" <td>35.0</td>\n",
" <td>29.0</td>\n",
" <td>36.7</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">2014</th>\n",
" <th>1</th>\n",
" <td>30.0</td>\n",
" <td>37.4</td>\n",
" <td>39.0</td>\n",
" <td>37.8</td>\n",
" <td>61.0</td>\n",
" <td>36.9</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>47.0</td>\n",
" <td>37.8</td>\n",
" <td>48.0</td>\n",
" <td>37.3</td>\n",
" <td>51.0</td>\n",
" <td>36.5</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
"subject Bob Guido Sue \n",
"type HR Temp HR Temp HR Temp\n",
"year visit \n",
"2013 1 31.0 38.7 32.0 36.7 35.0 37.2\n",
" 2 44.0 37.7 50.0 35.0 29.0 36.7\n",
"2014 1 30.0 37.4 39.0 37.8 61.0 36.9\n",
" 2 47.0 37.8 48.0 37.3 51.0 36.5"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"health_data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Remember that columns are primary in a ``DataFrame``, and the syntax used for multiply indexed ``Series`` applies to the columns.\n",
"For example, we can recover Guido's heart rate data with a simple operation:"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"year visit\n",
"2013 1 32.0\n",
" 2 50.0\n",
"2014 1 39.0\n",
" 2 48.0\n",
"Name: (Guido, HR), dtype: float64"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"health_data['Guido', 'HR']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Also, as with the single-index case, we can use the ``loc``, ``iloc``, and ``ix`` indexers introduced in [Data Indexing and Selection](03.02-Data-Indexing-and-Selection.ipynb). For example:"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr>\n",
" <th></th>\n",
" <th>subject</th>\n",
" <th colspan=\"2\" halign=\"left\">Bob</th>\n",
" </tr>\n",
" <tr>\n",
" <th></th>\n",
" <th>type</th>\n",
" <th>HR</th>\n",
" <th>Temp</th>\n",
" </tr>\n",
" <tr>\n",
" <th>year</th>\n",
" <th>visit</th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">2013</th>\n",
" <th>1</th>\n",
" <td>31.0</td>\n",
" <td>38.7</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>44.0</td>\n",
" <td>37.7</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
"subject Bob \n",
"type HR Temp\n",
"year visit \n",
"2013 1 31.0 38.7\n",
" 2 44.0 37.7"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"health_data.iloc[:2, :2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"These indexers provide an array-like view of the underlying two-dimensional data, but each individual index in ``loc`` or ``iloc`` can be passed a tuple of multiple indices. For example:"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"year visit\n",
"2013 1 31.0\n",
" 2 44.0\n",
"2014 1 30.0\n",
" 2 47.0\n",
"Name: (Bob, HR), dtype: float64"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"health_data.loc[:, ('Bob', 'HR')]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Working with slices within these index tuples is not especially convenient; trying to create a slice within a tuple will lead to a syntax error:"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "invalid syntax (<ipython-input-32-8e3cc151e316>, line 1)",
"output_type": "error",
"traceback": [
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-32-8e3cc151e316>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m health_data.loc[(:, 1), (:, 'HR')]\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
]
}
],
"source": [
"health_data.loc[(:, 1), (:, 'HR')]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You could get around this by building the desired slice explicitly using Python's built-in ``slice()`` function, but a better way in this context is to use an ``IndexSlice`` object, which Pandas provides for precisely this situation.\n",
"For example:"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr>\n",
" <th></th>\n",
" <th>subject</th>\n",
" <th>Bob</th>\n",
" <th>Guido</th>\n",
" <th>Sue</th>\n",
" </tr>\n",
" <tr>\n",
" <th></th>\n",
" <th>type</th>\n",
" <th>HR</th>\n",
" <th>HR</th>\n",
" <th>HR</th>\n",
" </tr>\n",
" <tr>\n",
" <th>year</th>\n",
" <th>visit</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2013</th>\n",
" <th>1</th>\n",
" <td>31.0</td>\n",
" <td>32.0</td>\n",
" <td>35.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014</th>\n",
" <th>1</th>\n",
" <td>30.0</td>\n",
" <td>39.0</td>\n",
" <td>61.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
"subject Bob Guido Sue\n",
"type HR HR HR\n",
"year visit \n",
"2013 1 31.0 32.0 35.0\n",
"2014 1 30.0 39.0 61.0"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"idx = pd.IndexSlice\n",
"health_data.loc[idx[:, 1], idx[:, 'HR']]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There are so many ways to interact with data in multiply indexed ``Series`` and ``DataFrame``s, and as with many tools in this book the best way to become familiar with them is to try them out!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Rearranging Multi-Indices\n",
"\n",
"One of the keys to working with multiply indexed data is knowing how to effectively transform the data.\n",
"There are a number of operations that will preserve all the information in the dataset, but rearrange it for the purposes of various computations.\n",
"We saw a brief example of this in the ``stack()`` and ``unstack()`` methods, but there are many more ways to finely control the rearrangement of data between hierarchical indices and columns, and we'll explore them here."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Sorted and unsorted indices\n",
"\n",
"Earlier, we briefly mentioned a caveat, but we should emphasize it more here.\n",
"*Many of the ``MultiIndex`` slicing operations will fail if the index is not sorted.*\n",
"Let's take a look at this here.\n",
"\n",
"We'll start by creating some simple multiply indexed data where the indices are *not lexographically sorted*:"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"char int\n",
"a 1 0.003001\n",
" 2 0.164974\n",
"c 1 0.741650\n",
" 2 0.569264\n",
"b 1 0.001693\n",
" 2 0.526226\n",
"dtype: float64"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"index = pd.MultiIndex.from_product([['a', 'c', 'b'], [1, 2]])\n",
"data = pd.Series(np.random.rand(6), index=index)\n",
"data.index.names = ['char', 'int']\n",
"data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If we try to take a partial slice of this index, it will result in an error:"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'KeyError'>\n",
"'Key length (1) was greater than MultiIndex lexsort depth (0)'\n"
]
}
],
"source": [
"try:\n",
" data['a':'b']\n",
"except KeyError as e:\n",
" print(type(e))\n",
" print(e)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Although it is not entirely clear from the error message, this is the result of the MultiIndex not being sorted.\n",
"For various reasons, partial slices and other similar operations require the levels in the ``MultiIndex`` to be in sorted (i.e., lexographical) order.\n",
"Pandas provides a number of convenience routines to perform this type of sorting; examples are the ``sort_index()`` and ``sortlevel()`` methods of the ``DataFrame``.\n",
"We'll use the simplest, ``sort_index()``, here:"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"char int\n",
"a 1 0.003001\n",
" 2 0.164974\n",
"b 1 0.001693\n",
" 2 0.526226\n",
"c 1 0.741650\n",
" 2 0.569264\n",
"dtype: float64"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = data.sort_index()\n",
"data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"With the index sorted in this way, partial slicing will work as expected:"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"char int\n",
"a 1 0.003001\n",
" 2 0.164974\n",
"b 1 0.001693\n",
" 2 0.526226\n",
"dtype: float64"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data['a':'b']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Stacking and unstacking indices\n",
"\n",
"As we saw briefly before, it is possible to convert a dataset from a stacked multi-index to a simple two-dimensional representation, optionally specifying the level to use:"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th>state</th>\n",
" <th>California</th>\n",
" <th>New York</th>\n",
" <th>Texas</th>\n",
" </tr>\n",
" <tr>\n",
" <th>year</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2000</th>\n",
" <td>33871648</td>\n",
" <td>18976457</td>\n",
" <td>20851820</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2010</th>\n",
" <td>37253956</td>\n",
" <td>19378102</td>\n",
" <td>25145561</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
"state California New York Texas\n",
"year \n",
"2000 33871648 18976457 20851820\n",
"2010 37253956 19378102 25145561"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop.unstack(level=0)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th>year</th>\n",
" <th>2000</th>\n",
" <th>2010</th>\n",
" </tr>\n",
" <tr>\n",
" <th>state</th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>California</th>\n",
" <td>33871648</td>\n",
" <td>37253956</td>\n",
" </tr>\n",
" <tr>\n",
" <th>New York</th>\n",
" <td>18976457</td>\n",
" <td>19378102</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Texas</th>\n",
" <td>20851820</td>\n",
" <td>25145561</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
"year 2000 2010\n",
"state \n",
"California 33871648 37253956\n",
"New York 18976457 19378102\n",
"Texas 20851820 25145561"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop.unstack(level=1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The opposite of ``unstack()`` is ``stack()``, which here can be used to recover the original series:"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"state year\n",
"California 2000 33871648\n",
" 2010 37253956\n",
"New York 2000 18976457\n",
" 2010 19378102\n",
"Texas 2000 20851820\n",
" 2010 25145561\n",
"dtype: int64"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop.unstack().stack()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Index setting and resetting\n",
"\n",
"Another way to rearrange hierarchical data is to turn the index labels into columns; this can be accomplished with the ``reset_index`` method.\n",
"Calling this on the population dictionary will result in a ``DataFrame`` with a *state* and *year* column holding the information that was formerly in the index.\n",
"For clarity, we can optionally specify the name of the data for the column representation:"
]
},
{
"cell_type": "code",
"execution_count": 41,
"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>state</th>\n",
" <th>year</th>\n",
" <th>population</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>California</td>\n",
" <td>2000</td>\n",
" <td>33871648</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>California</td>\n",
" <td>2010</td>\n",
" <td>37253956</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>New York</td>\n",
" <td>2000</td>\n",
" <td>18976457</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>New York</td>\n",
" <td>2010</td>\n",
" <td>19378102</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Texas</td>\n",
" <td>2000</td>\n",
" <td>20851820</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>Texas</td>\n",
" <td>2010</td>\n",
" <td>25145561</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" state year population\n",
"0 California 2000 33871648\n",
"1 California 2010 37253956\n",
"2 New York 2000 18976457\n",
"3 New York 2010 19378102\n",
"4 Texas 2000 20851820\n",
"5 Texas 2010 25145561"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop_flat = pop.reset_index(name='population')\n",
"pop_flat"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Often when working with data in the real world, the raw input data looks like this and it's useful to build a ``MultiIndex`` from the column values.\n",
"This can be done with the ``set_index`` method of the ``DataFrame``, which returns a multiply indexed ``DataFrame``:"
]
},
{
"cell_type": "code",
"execution_count": 42,
"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>population</th>\n",
" </tr>\n",
" <tr>\n",
" <th>state</th>\n",
" <th>year</th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">California</th>\n",
" <th>2000</th>\n",
" <td>33871648</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2010</th>\n",
" <td>37253956</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">New York</th>\n",
" <th>2000</th>\n",
" <td>18976457</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2010</th>\n",
" <td>19378102</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">Texas</th>\n",
" <th>2000</th>\n",
" <td>20851820</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2010</th>\n",
" <td>25145561</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" population\n",
"state year \n",
"California 2000 33871648\n",
" 2010 37253956\n",
"New York 2000 18976457\n",
" 2010 19378102\n",
"Texas 2000 20851820\n",
" 2010 25145561"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop_flat.set_index(['state', 'year'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In practice, I find this type of reindexing to be one of the more useful patterns when encountering real-world datasets."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Data Aggregations on Multi-Indices\n",
"\n",
"We've previously seen that Pandas has built-in data aggregation methods, such as ``mean()``, ``sum()``, and ``max()``.\n",
"For hierarchically indexed data, these can be passed a ``level`` parameter that controls which subset of the data the aggregate is computed on.\n",
"\n",
"For example, let's return to our health data:"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr>\n",
" <th></th>\n",
" <th>subject</th>\n",
" <th colspan=\"2\" halign=\"left\">Bob</th>\n",
" <th colspan=\"2\" halign=\"left\">Guido</th>\n",
" <th colspan=\"2\" halign=\"left\">Sue</th>\n",
" </tr>\n",
" <tr>\n",
" <th></th>\n",
" <th>type</th>\n",
" <th>HR</th>\n",
" <th>Temp</th>\n",
" <th>HR</th>\n",
" <th>Temp</th>\n",
" <th>HR</th>\n",
" <th>Temp</th>\n",
" </tr>\n",
" <tr>\n",
" <th>year</th>\n",
" <th>visit</th>\n",
" <th></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 rowspan=\"2\" valign=\"top\">2013</th>\n",
" <th>1</th>\n",
" <td>31.0</td>\n",
" <td>38.7</td>\n",
" <td>32.0</td>\n",
" <td>36.7</td>\n",
" <td>35.0</td>\n",
" <td>37.2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>44.0</td>\n",
" <td>37.7</td>\n",
" <td>50.0</td>\n",
" <td>35.0</td>\n",
" <td>29.0</td>\n",
" <td>36.7</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"2\" valign=\"top\">2014</th>\n",
" <th>1</th>\n",
" <td>30.0</td>\n",
" <td>37.4</td>\n",
" <td>39.0</td>\n",
" <td>37.8</td>\n",
" <td>61.0</td>\n",
" <td>36.9</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>47.0</td>\n",
" <td>37.8</td>\n",
" <td>48.0</td>\n",
" <td>37.3</td>\n",
" <td>51.0</td>\n",
" <td>36.5</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
"subject Bob Guido Sue \n",
"type HR Temp HR Temp HR Temp\n",
"year visit \n",
"2013 1 31.0 38.7 32.0 36.7 35.0 37.2\n",
" 2 44.0 37.7 50.0 35.0 29.0 36.7\n",
"2014 1 30.0 37.4 39.0 37.8 61.0 36.9\n",
" 2 47.0 37.8 48.0 37.3 51.0 36.5"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"health_data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Perhaps we'd like to average-out the measurements in the two visits each year. We can do this by naming the index level we'd like to explore, in this case the year:"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr>\n",
" <th>subject</th>\n",
" <th colspan=\"2\" halign=\"left\">Bob</th>\n",
" <th colspan=\"2\" halign=\"left\">Guido</th>\n",
" <th colspan=\"2\" halign=\"left\">Sue</th>\n",
" </tr>\n",
" <tr>\n",
" <th>type</th>\n",
" <th>HR</th>\n",
" <th>Temp</th>\n",
" <th>HR</th>\n",
" <th>Temp</th>\n",
" <th>HR</th>\n",
" <th>Temp</th>\n",
" </tr>\n",
" <tr>\n",
" <th>year</th>\n",
" <th></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>2013</th>\n",
" <td>37.5</td>\n",
" <td>38.2</td>\n",
" <td>41.0</td>\n",
" <td>35.85</td>\n",
" <td>32.0</td>\n",
" <td>36.95</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014</th>\n",
" <td>38.5</td>\n",
" <td>37.6</td>\n",
" <td>43.5</td>\n",
" <td>37.55</td>\n",
" <td>56.0</td>\n",
" <td>36.70</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
"subject Bob Guido Sue \n",
"type HR Temp HR Temp HR Temp\n",
"year \n",
"2013 37.5 38.2 41.0 35.85 32.0 36.95\n",
"2014 38.5 37.6 43.5 37.55 56.0 36.70"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data_mean = health_data.mean(level='year')\n",
"data_mean"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"By further making use of the ``axis`` keyword, we can take the mean among levels on the columns as well:"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th>type</th>\n",
" <th>HR</th>\n",
" <th>Temp</th>\n",
" </tr>\n",
" <tr>\n",
" <th>year</th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2013</th>\n",
" <td>36.833333</td>\n",
" <td>37.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014</th>\n",
" <td>46.000000</td>\n",
" <td>37.283333</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
"type HR Temp\n",
"year \n",
"2013 36.833333 37.000000\n",
"2014 46.000000 37.283333"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data_mean.mean(axis=1, level='type')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Thus in two lines, we've been able to find the average heart rate and temperature measured among all subjects in all visits each year.\n",
"This syntax is actually a short cut to the ``GroupBy`` functionality, which we will discuss in [Aggregation and Grouping](03.08-Aggregation-and-Grouping.ipynb).\n",
"While this is a toy example, many real-world datasets have similar hierarchical structure."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Aside: Panel Data\n",
"\n",
"Pandas has a few other fundamental data structures that we have not yet discussed, namely the ``pd.Panel`` and ``pd.Panel4D`` objects.\n",
"These can be thought of, respectively, as three-dimensional and four-dimensional generalizations of the (one-dimensional) ``Series`` and (two-dimensional) ``DataFrame`` structures.\n",
"Once you are familiar with indexing and manipulation of data in a ``Series`` and ``DataFrame``, ``Panel`` and ``Panel4D`` are relatively straightforward to use.\n",
"In particular, the ``ix``, ``loc``, and ``iloc`` indexers discussed in [Data Indexing and Selection](03.02-Data-Indexing-and-Selection.ipynb) extend readily to these higher-dimensional structures.\n",
"\n",
"We won't cover these panel structures further in this text, as I've found in the majority of cases that multi-indexing is a more useful and conceptually simpler representation for higher-dimensional data.\n",
"Additionally, panel data is fundamentally a dense data representation, while multi-indexing is fundamentally a sparse data representation.\n",
"As the number of dimensions increases, the dense representation can become very inefficient for the majority of real-world datasets.\n",
"For the occasional specialized application, however, these structures can be useful.\n",
"If you'd like to read more about the ``Panel`` and ``Panel4D`` structures, see the references listed in [Further Resources](03.13-Further-Resources.ipynb)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!--NAVIGATION-->\n",
"< [Handling Missing Data](03.04-Missing-Values.ipynb) | [Contents](Index.ipynb) | [Combining Datasets: Concat and Append](03.06-Concat-And-Append.ipynb) >"
]
}
],
"metadata": {
"anaconda-cloud": {},
"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.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}