{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\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": [ "\n", "< [Introducing Pandas Objects](03.01-Introducing-Pandas-Objects.ipynb) | [Contents](Index.ipynb) | [Operating on Data in Pandas](03.03-Operations-in-Pandas.ipynb) >" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Data Indexing and Selection" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In [Chapter 2](02.00-Introduction-to-NumPy.ipynb), we looked in detail at methods and tools to access, set, and modify values in NumPy arrays.\n", "These included indexing (e.g., ``arr[2, 1]``), slicing (e.g., ``arr[:, 1:5]``), masking (e.g., ``arr[arr > 0]``), fancy indexing (e.g., ``arr[0, [1, 5]]``), and combinations thereof (e.g., ``arr[:, [1, 5]]``).\n", "Here we'll look at similar means of accessing and modifying values in Pandas ``Series`` and ``DataFrame`` objects.\n", "If you have used the NumPy patterns, the corresponding patterns in Pandas will feel very familiar, though there are a few quirks to be aware of.\n", "\n", "We'll start with the simple case of the one-dimensional ``Series`` object, and then move on to the more complicated two-dimesnional ``DataFrame`` object." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Data Selection in Series\n", "\n", "As we saw in the previous section, a ``Series`` object acts in many ways like a one-dimensional NumPy array, and in many ways like a standard Python dictionary.\n", "If we keep these two overlapping analogies in mind, it will help us to understand the patterns of data indexing and selection in these arrays." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Series as dictionary\n", "\n", "Like a dictionary, the ``Series`` object provides a mapping from a collection of keys to a collection of values:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "a 0.25\n", "b 0.50\n", "c 0.75\n", "d 1.00\n", "dtype: float64" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "data = pd.Series([0.25, 0.5, 0.75, 1.0],\n", " index=['a', 'b', 'c', 'd'])\n", "data" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.5" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data['b']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also use dictionary-like Python expressions and methods to examine the keys/indices and values:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'a' in data" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "Index(['a', 'b', 'c', 'd'], dtype='object')" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.keys()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[('a', 0.25), ('b', 0.5), ('c', 0.75), ('d', 1.0)]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(data.items())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "``Series`` objects can even be modified with a dictionary-like syntax.\n", "Just as you can extend a dictionary by assigning to a new key, you can extend a ``Series`` by assigning to a new index value:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "a 0.25\n", "b 0.50\n", "c 0.75\n", "d 1.00\n", "e 1.25\n", "dtype: float64" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data['e'] = 1.25\n", "data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This easy mutability of the objects is a convenient feature: under the hood, Pandas is making decisions about memory layout and data copying that might need to take place; the user generally does not need to worry about these issues." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Series as one-dimensional array" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A ``Series`` builds on this dictionary-like interface and provides array-style item selection via the same basic mechanisms as NumPy arrays – that is, *slices*, *masking*, and *fancy indexing*.\n", "Examples of these are as follows:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "a 0.25\n", "b 0.50\n", "c 0.75\n", "dtype: float64" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# slicing by explicit index\n", "data['a':'c']" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "a 0.25\n", "b 0.50\n", "dtype: float64" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# slicing by implicit integer index\n", "data[0:2]" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "b 0.50\n", "c 0.75\n", "dtype: float64" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# masking\n", "data[(data > 0.3) & (data < 0.8)]" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "a 0.25\n", "e 1.25\n", "dtype: float64" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# fancy indexing\n", "data[['a', 'e']]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Among these, slicing may be the source of the most confusion.\n", "Notice that when slicing with an explicit index (i.e., ``data['a':'c']``), the final index is *included* in the slice, while when slicing with an implicit index (i.e., ``data[0:2]``), the final index is *excluded* from the slice." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Indexers: loc, iloc, and ix\n", "\n", "These slicing and indexing conventions can be a source of confusion.\n", "For example, if your ``Series`` has an explicit integer index, an indexing operation such as ``data[1]`` will use the explicit indices, while a slicing operation like ``data[1:3]`` will use the implicit Python-style index." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1 a\n", "3 b\n", "5 c\n", "dtype: object" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = pd.Series(['a', 'b', 'c'], index=[1, 3, 5])\n", "data" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'a'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# explicit index when indexing\n", "data[1]" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "3 b\n", "5 c\n", "dtype: object" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# implicit index when slicing\n", "data[1:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Because of this potential confusion in the case of integer indexes, Pandas provides some special *indexer* attributes that explicitly expose certain indexing schemes.\n", "These are not functional methods, but attributes that expose a particular slicing interface to the data in the ``Series``.\n", "\n", "First, the ``loc`` attribute allows indexing and slicing that always references the explicit index:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'a'" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.loc[1]" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1 a\n", "3 b\n", "dtype: object" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.loc[1:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The ``iloc`` attribute allows indexing and slicing that always references the implicit Python-style index:" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'b'" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.iloc[1]" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "3 b\n", "5 c\n", "dtype: object" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.iloc[1:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A third indexing attribute, ``ix``, is a hybrid of the two, and for ``Series`` objects is equivalent to standard ``[]``-based indexing.\n", "The purpose of the ``ix`` indexer will become more apparent in the context of ``DataFrame`` objects, which we will discuss in a moment.\n", "\n", "One guiding principle of Python code is that \"explicit is better than implicit.\"\n", "The explicit nature of ``loc`` and ``iloc`` make them very useful in maintaining clean and readable code; especially in the case of integer indexes, I recommend using these both to make code easier to read and understand, and to prevent subtle bugs due to the mixed indexing/slicing convention." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Data Selection in DataFrame\n", "\n", "Recall that a ``DataFrame`` acts in many ways like a two-dimensional or structured array, and in other ways like a dictionary of ``Series`` structures sharing the same index.\n", "These analogies can be helpful to keep in mind as we explore data selection within this structure." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### DataFrame as a dictionary\n", "\n", "The first analogy we will consider is the ``DataFrame`` as a dictionary of related ``Series`` objects.\n", "Let's return to our example of areas and populations of states:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
areapop
California42396738332521
Florida17031219552860
Illinois14999512882135
New York14129719651127
Texas69566226448193
\n", "
" ], "text/plain": [ " area pop\n", "California 423967 38332521\n", "Florida 170312 19552860\n", "Illinois 149995 12882135\n", "New York 141297 19651127\n", "Texas 695662 26448193" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "area = pd.Series({'California': 423967, 'Texas': 695662,\n", " 'New York': 141297, 'Florida': 170312,\n", " 'Illinois': 149995})\n", "pop = pd.Series({'California': 38332521, 'Texas': 26448193,\n", " 'New York': 19651127, 'Florida': 19552860,\n", " 'Illinois': 12882135})\n", "data = pd.DataFrame({'area':area, 'pop':pop})\n", "data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The individual ``Series`` that make up the columns of the ``DataFrame`` can be accessed via dictionary-style indexing of the column name:" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "California 423967\n", "Florida 170312\n", "Illinois 149995\n", "New York 141297\n", "Texas 695662\n", "Name: area, dtype: int64" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data['area']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Equivalently, we can use attribute-style access with column names that are strings:" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "California 423967\n", "Florida 170312\n", "Illinois 149995\n", "New York 141297\n", "Texas 695662\n", "Name: area, dtype: int64" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.area" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This attribute-style column access actually accesses the exact same object as the dictionary-style access:" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.area is data['area']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Though this is a useful shorthand, keep in mind that it does not work for all cases!\n", "For example, if the column names are not strings, or if the column names conflict with methods of the ``DataFrame``, this attribute-style access is not possible.\n", "For example, the ``DataFrame`` has a ``pop()`` method, so ``data.pop`` will point to this rather than the ``\"pop\"`` column:" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.pop is data['pop']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In particular, you should avoid the temptation to try column assignment via attribute (i.e., use ``data['pop'] = z`` rather than ``data.pop = z``).\n", "\n", "Like with the ``Series`` objects discussed earlier, this dictionary-style syntax can also be used to modify the object, in this case adding a new column:" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
areapopdensity
California4239673833252190.413926
Florida17031219552860114.806121
Illinois1499951288213585.883763
New York14129719651127139.076746
Texas6956622644819338.018740
\n", "
" ], "text/plain": [ " area pop density\n", "California 423967 38332521 90.413926\n", "Florida 170312 19552860 114.806121\n", "Illinois 149995 12882135 85.883763\n", "New York 141297 19651127 139.076746\n", "Texas 695662 26448193 38.018740" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data['density'] = data['pop'] / data['area']\n", "data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This shows a preview of the straightforward syntax of element-by-element arithmetic between ``Series`` objects; we'll dig into this further in [Operating on Data in Pandas](03.03-Operations-in-Pandas.ipynb)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### DataFrame as two-dimensional array\n", "\n", "As mentioned previously, we can also view the ``DataFrame`` as an enhanced two-dimensional array.\n", "We can examine the raw underlying data array using the ``values`` attribute:" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 4.23967000e+05, 3.83325210e+07, 9.04139261e+01],\n", " [ 1.70312000e+05, 1.95528600e+07, 1.14806121e+02],\n", " [ 1.49995000e+05, 1.28821350e+07, 8.58837628e+01],\n", " [ 1.41297000e+05, 1.96511270e+07, 1.39076746e+02],\n", " [ 6.95662000e+05, 2.64481930e+07, 3.80187404e+01]])" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.values" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With this picture in mind, many familiar array-like observations can be done on the ``DataFrame`` itself.\n", "For example, we can transpose the full ``DataFrame`` to swap rows and columns:" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CaliforniaFloridaIllinoisNew YorkTexas
area4.239670e+051.703120e+051.499950e+051.412970e+056.956620e+05
pop3.833252e+071.955286e+071.288214e+071.965113e+072.644819e+07
density9.041393e+011.148061e+028.588376e+011.390767e+023.801874e+01
\n", "
" ], "text/plain": [ " California Florida Illinois New York Texas\n", "area 4.239670e+05 1.703120e+05 1.499950e+05 1.412970e+05 6.956620e+05\n", "pop 3.833252e+07 1.955286e+07 1.288214e+07 1.965113e+07 2.644819e+07\n", "density 9.041393e+01 1.148061e+02 8.588376e+01 1.390767e+02 3.801874e+01" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.T" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When it comes to indexing of ``DataFrame`` objects, however, it is clear that the dictionary-style indexing of columns precludes our ability to simply treat it as a NumPy array.\n", "In particular, passing a single index to an array accesses a row:" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 4.23967000e+05, 3.83325210e+07, 9.04139261e+01])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.values[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and passing a single \"index\" to a ``DataFrame`` accesses a column:" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "California 423967\n", "Florida 170312\n", "Illinois 149995\n", "New York 141297\n", "Texas 695662\n", "Name: area, dtype: int64" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data['area']" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "Thus for array-style indexing, we need another convention.\n", "Here Pandas again uses the ``loc``, ``iloc``, and ``ix`` indexers mentioned earlier.\n", "Using the ``iloc`` indexer, we can index the underlying array as if it is a simple NumPy array (using the implicit Python-style index), but the ``DataFrame`` index and column labels are maintained in the result:" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
areapop
California42396738332521
Florida17031219552860
Illinois14999512882135
\n", "
" ], "text/plain": [ " area pop\n", "California 423967 38332521\n", "Florida 170312 19552860\n", "Illinois 149995 12882135" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.iloc[:3, :2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Similarly, using the ``loc`` indexer we can index the underlying data in an array-like style but using the explicit index and column names:" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
areapop
California42396738332521
Florida17031219552860
Illinois14999512882135
\n", "
" ], "text/plain": [ " area pop\n", "California 423967 38332521\n", "Florida 170312 19552860\n", "Illinois 149995 12882135" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.loc[:'Illinois', :'pop']" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "The ``ix`` indexer allows a hybrid of these two approaches:" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
areapop
California42396738332521
Florida17031219552860
Illinois14999512882135
\n", "
" ], "text/plain": [ " area pop\n", "California 423967 38332521\n", "Florida 170312 19552860\n", "Illinois 149995 12882135" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.ix[:3, :'pop']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Keep in mind that for integer indices, the ``ix`` indexer is subject to the same potential sources of confusion as discussed for integer-indexed ``Series`` objects.\n", "\n", "Any of the familiar NumPy-style data access patterns can be used within these indexers.\n", "For example, in the ``loc`` indexer we can combine masking and fancy indexing as in the following:" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
popdensity
Florida19552860114.806121
New York19651127139.076746
\n", "
" ], "text/plain": [ " pop density\n", "Florida 19552860 114.806121\n", "New York 19651127 139.076746" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.loc[data.density > 100, ['pop', 'density']]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Any of these indexing conventions may also be used to set or modify values; this is done in the standard way that you might be accustomed to from working with NumPy:" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
areapopdensity
California4239673833252190.000000
Florida17031219552860114.806121
Illinois1499951288213585.883763
New York14129719651127139.076746
Texas6956622644819338.018740
\n", "
" ], "text/plain": [ " area pop density\n", "California 423967 38332521 90.000000\n", "Florida 170312 19552860 114.806121\n", "Illinois 149995 12882135 85.883763\n", "New York 141297 19651127 139.076746\n", "Texas 695662 26448193 38.018740" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.iloc[0, 2] = 90\n", "data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To build up your fluency in Pandas data manipulation, I suggest spending some time with a simple ``DataFrame`` and exploring the types of indexing, slicing, masking, and fancy indexing that are allowed by these various indexing approaches." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Additional indexing conventions\n", "\n", "There are a couple extra indexing conventions that might seem at odds with the preceding discussion, but nevertheless can be very useful in practice.\n", "First, while *indexing* refers to columns, *slicing* refers to rows:" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
areapopdensity
Florida17031219552860114.806121
Illinois1499951288213585.883763
\n", "
" ], "text/plain": [ " area pop density\n", "Florida 170312 19552860 114.806121\n", "Illinois 149995 12882135 85.883763" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data['Florida':'Illinois']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Such slices can also refer to rows by number rather than by index:" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
areapopdensity
Florida17031219552860114.806121
Illinois1499951288213585.883763
\n", "
" ], "text/plain": [ " area pop density\n", "Florida 170312 19552860 114.806121\n", "Illinois 149995 12882135 85.883763" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data[1:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Similarly, direct masking operations are also interpreted row-wise rather than column-wise:" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
areapopdensity
Florida17031219552860114.806121
New York14129719651127139.076746
\n", "
" ], "text/plain": [ " area pop density\n", "Florida 170312 19552860 114.806121\n", "New York 141297 19651127 139.076746" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data[data.density > 100]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "These two conventions are syntactically similar to those on a NumPy array, and while these may not precisely fit the mold of the Pandas conventions, they are nevertheless quite useful in practice." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "< [Introducing Pandas Objects](03.01-Introducing-Pandas-Objects.ipynb) | [Contents](Index.ipynb) | [Operating on Data in Pandas](03.03-Operations-in-Pandas.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 }