03.02-Data-Indexing-and-Selection.py - #!/usr/bin/env...

This preview shows page 1 - 3 out of 8 pages.

#!/usr/bin/env python# coding: utf-8# <!--BOOK_INFORMATION--># <img align="left" style="padding-right:10px;" src="figures/PDSH-cover-small.png">## *This notebook contains an excerpt from the [Python Data Science Handbook]() by Jake VanderPlas; thecontent is available [on GitHub]().*## *The text is released under the [CC-BY-NC-ND license](), and code isreleased under the [MIT license](). If youfind this content useful, please consider supporting the work by [buying thebook]()!*# <!--NAVIGATION--># < [Introducing Pandas Objects](03.01-Introducing-Pandas-Objects.ipynb) |[Contents](Index.ipynb) | [Operating on Data in Pandas](03.03-Operations-in-Pandas.ipynb) >## <ahref="/blob/master/notebooks/03.02-Data-Indexing-and-Selection.ipynb"><imgalign="left" src=""alt="Open in Colab" title="Open and Execute in Google Colaboratory"></a>## # Data Indexing and Selection# In [Chapter 2](02.00-Introduction-to-NumPy.ipynb), we looked in detail atmethods and tools to access, set, and modify values in NumPy arrays.# 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]]``).# Here we'll look at similar means of accessing and modifying values in Pandas``Series`` and ``DataFrame`` objects.# If you have used the NumPy patterns, the corresponding patterns in Pandas willfeel very familiar, though there are a few quirks to be aware of.## We'll start with the simple case of the one-dimensional ``Series`` object, andthen move on to the more complicated two-dimesnional ``DataFrame`` object.# ## Data Selection in Series## As we saw in the previous section, a ``Series`` object acts in many ways likea one-dimensional NumPy array, and in many ways like a standard Pythondictionary.# If we keep these two overlapping analogies in mind, it will help us tounderstand the patterns of data indexing and selection in these arrays.# ### Series as dictionary## Like a dictionary, the ``Series`` object provides a mapping from a collectionof keys to a collection of values:# In[1]:import pandas as pddata = pd.Series([0.25, 0.5, 0.75, 1.0],index=['a', 'b', 'c', 'd'])
data# In[2]:data['b']# We can also use dictionary-like Python expressions and methods to examine thekeys/indices and values:# In[3]:'a' in data# In[4]:data.keys()# In[5]:list(data.items())# ``Series`` objects can even be modified with a dictionary-like syntax.

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 8 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Spring
Professor
NoProfessor
Tags
Array

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture