{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Accessing a Series\n", "\n", "There are multiple ways to get to the data that is stored in your `Series`. Let's explore the **`balances`** `Series`. \n", "\n", "Remember, the `Series` is indexed by username. The label is the username, the value is that user's balance." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Setup\n", "import pandas as pd\n", "\n", "from utils import render\n", "\n", "\n", "# Standard Python dictionary\n", "test_balance_data = {\n", " 'pasan': 20.00,\n", " 'treasure': 20.18,\n", " 'ashley': 1.05,\n", " 'craig': 42.42,\n", "}\n", "\n", "balances = pd.Series(test_balance_data)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Accessing by Index\n", "\n", "A `Series` is ordered and indexable. It is zero based and you can access it by index, just like you would a list or array." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "20.0" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get the first user's balance\n", "balances[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's take a loook at the value returned." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "numpy.float64" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(balances[0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The value is wrapped in a `NumPy.Scalar` so that it keeps it's data type and will play well with others.\n", "\n", "The same positional indexing works just as it does with a standard list." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42.42" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# The last balance\n", "balances[-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Accessing by Label\n", "Since a series is labelled, you can also access it much like you would a standard `dict`." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "20.0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "balances['pasan']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `Series` behave like dictionaries" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "Accessing a non-existent key raises a `KeyError`." ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "try:\n", " balances['kermit']\n", "except KeyError:\n", " render('Accessing a non-existent key raises a `KeyError`.')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "Use `get` to safely access keys. `None` is returned if key not present." ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "if balances.get('kermit') is None:\n", " render('Use `get` to safely access keys. `None` is returned if key not present.')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "Use `in` to test the existence of a label." ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "if 'kermit' not in balances:\n", " render('Use `in` to test the existence of a label.')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Accessing by Property\n", "As long as your label meets variable naming constraints, it will be available as a property via dot notation on the `Series`!" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.05" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "balances.ashley" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Accessing More Explicitly\n", "\n", "We are using indexing which can *either* be a label *or* a positional index. This can get confusing. It's possible to be more explicit, [which yes wise Pythonista](https://www.python.org/dev/peps/pep-0020/), is always better than implicit.\n", "\n", "A `Series` exposes a property named `loc` which can be used to explicitly lookup by label based indices only." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "20.0" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "balances.loc['pasan']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And to use the positional index explicitly, you can use the property `iloc`." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "20.0" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get the first value\n", "balances.iloc[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Accessing by Slice\n", "Like a NumPy array, a `Series` also provides a way to use slices to get different portions of the data, returned as a `Series`. \n", "\n", "*NOTE*: Slicing with indices vs. labels behaves differently. The latter is inclusive." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Slicing by Positional Index\n", "When using positional indices, the slice is exclusive..." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "pasan 20.00\n", "treasure 20.18\n", "ashley 1.05\n", "dtype: float64" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Includes values from zero\n", "# up until **and not** including 3\n", "balances.iloc[0:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Slicing by Label\n", "When using labels, the slice is inclusive..." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "pasan 20.00\n", "treasure 20.18\n", "ashley 1.05\n", "dtype: float64" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Include the values starting at 'pasan' \n", "# up until **and** including 'ashley'\n", "balances.loc['pasan':'ashley']" ] } ], "metadata": { "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.7.0" } }, "nbformat": 4, "nbformat_minor": 2 }