{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Operating on Data in Pandas" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One of the strengths of NumPy is that it allows us to perform quick element-wise operations, both with basic arithmetic (addition, subtraction, multiplication, etc.) and with more complicated operations (trigonometric functions, exponential and logarithmic functions, etc.).\n", "Pandas inherits much of this functionality from NumPy, and the ufuncs introduced in [Computation on NumPy Arrays: Universal Functions](02.03-Computation-on-arrays-ufuncs.ipynb) are key to this.\n", "\n", "Pandas includes a couple of useful twists, however: for unary operations like negation and trigonometric functions, these ufuncs will *preserve index and column labels* in the output, and for binary operations such as addition and multiplication, Pandas will automatically *align indices* when passing the objects to the ufunc.\n", "This means that keeping the context of data and combining data from different sources—both potentially error-prone tasks with raw NumPy arrays—become essentially foolproof with Pandas.\n", "We will additionally see that there are well-defined operations between one-dimensional `Series` structures and two-dimensional `DataFrame` structures." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ufuncs: Index Preservation\n", "\n", "Because Pandas is designed to work with NumPy, any NumPy ufunc will work on Pandas `Series` and `DataFrame` objects.\n", "Let's start by defining a simple `Series` and `DataFrame` on which to demonstrate this:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "tags": [] }, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "0 0\n", "1 7\n", "2 6\n", "3 4\n", "dtype: int64" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rng = np.random.default_rng(42)\n", "ser = pd.Series(rng.integers(0, 10, 4))\n", "ser" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": 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", "
ABCD
04806
12059
27777
\n", "
" ], "text/plain": [ " A B C D\n", "0 4 8 0 6\n", "1 2 0 5 9\n", "2 7 7 7 7" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.DataFrame(rng.integers(0, 10, (3, 4)),\n", " columns=['A', 'B', 'C', 'D'])\n", "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we apply a NumPy ufunc on either of these objects, the result will be another Pandas object *with the indices preserved:*" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "0 1.000000\n", "1 1096.633158\n", "2 403.428793\n", "3 54.598150\n", "dtype: float64" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.exp(ser)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is true also for more involved sequences of operations:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": 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", "
ABCD
01.224647e-16-2.449294e-160.000000-1.000000
11.000000e+000.000000e+00-0.7071070.707107
2-7.071068e-01-7.071068e-01-0.707107-0.707107
\n", "
" ], "text/plain": [ " A B C D\n", "0 1.224647e-16 -2.449294e-16 0.000000 -1.000000\n", "1 1.000000e+00 0.000000e+00 -0.707107 0.707107\n", "2 -7.071068e-01 -7.071068e-01 -0.707107 -0.707107" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sin(df * np.pi / 4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Any of the ufuncs discussed in [Computation on NumPy Arrays: Universal Functions](02.03-Computation-on-arrays-ufuncs.ipynb) can be used in a similar manner." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ufuncs: Index Alignment\n", "\n", "For binary operations on two `Series` or `DataFrame` objects, Pandas will align indices in the process of performing the operation.\n", "This is very convenient when working with incomplete data, as we'll see in some of the examples that follow." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Index Alignment in Series\n", "\n", "As an example, suppose we are combining two different data sources and wish to find only the top three US states by *area* and the top three US states by *population*:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "area = pd.Series({'Alaska': 1723337, 'Texas': 695662,\n", " 'California': 423967}, name='area')\n", "population = pd.Series({'California': 39538223, 'Texas': 29145505,\n", " 'Florida': 21538187}, name='population')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's see what happens when we divide these to compute the population density:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "Alaska NaN\n", "California 93.257784\n", "Florida NaN\n", "Texas 41.896072\n", "dtype: float64" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "population / area" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The resulting array contains the *union* of indices of the two input arrays, which could be determined directly from these indices:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "Index(['Alaska', 'California', 'Florida', 'Texas'], dtype='object')" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "area.index.union(population.index)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Any item for which one or the other does not have an entry is marked with `NaN`, or \"Not a Number,\" which is how Pandas marks missing data (see further discussion of missing data in [Handling Missing Data](03.04-Missing-Values.ipynb)).\n", "This index matching is implemented this way for any of Python's built-in arithmetic expressions; any missing values are marked by `NaN`:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "0 NaN\n", "1 5.0\n", "2 9.0\n", "3 NaN\n", "dtype: float64" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = pd.Series([2, 4, 6], index=[0, 1, 2])\n", "B = pd.Series([1, 3, 5], index=[1, 2, 3])\n", "A + B" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If using `NaN` values is not the desired behavior, the fill value can be modified using appropriate object methods in place of the operators.\n", "For example, calling ``A.add(B)`` is equivalent to calling ``A + B``, but allows optional explicit specification of the fill value for any elements in ``A`` or ``B`` that might be missing:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "0 2.0\n", "1 5.0\n", "2 9.0\n", "3 5.0\n", "dtype: float64" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A.add(B, fill_value=0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Index Alignment in DataFrames\n", "\n", "A similar type of alignment takes place for *both* columns and indices when performing operations on `DataFrame` objects:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": 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", "
ab
0102
1169
\n", "
" ], "text/plain": [ " a b\n", "0 10 2\n", "1 16 9" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = pd.DataFrame(rng.integers(0, 20, (2, 2)),\n", " columns=['a', 'b'])\n", "A" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": 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", "
bac
0531
1976
2485
\n", "
" ], "text/plain": [ " b a c\n", "0 5 3 1\n", "1 9 7 6\n", "2 4 8 5" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "B = pd.DataFrame(rng.integers(0, 10, (3, 3)),\n", " columns=['b', 'a', 'c'])\n", "B" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": 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", "
abc
013.07.0NaN
123.018.0NaN
2NaNNaNNaN
\n", "
" ], "text/plain": [ " a b c\n", "0 13.0 7.0 NaN\n", "1 23.0 18.0 NaN\n", "2 NaN NaN NaN" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A + B" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that indices are aligned correctly irrespective of their order in the two objects, and indices in the result are sorted.\n", "As was the case with `Series`, we can use the associated object's arithmetic methods and pass any desired `fill_value` to be used in place of missing entries.\n", "Here we'll fill with the mean of all values in `A`:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": 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", "
abc
013.007.0010.25
123.0018.0015.25
217.2513.2514.25
\n", "
" ], "text/plain": [ " a b c\n", "0 13.00 7.00 10.25\n", "1 23.00 18.00 15.25\n", "2 17.25 13.25 14.25" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A.add(B, fill_value=A.values.mean())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following table lists Python operators and their equivalent Pandas object methods:\n", "\n", "| Python operator | Pandas method(s) |\n", "|-----------------|---------------------------------|\n", "| `+` | `add` |\n", "| `-` | `sub`, `subtract` |\n", "| `*` | `mul`, `multiply` |\n", "| `/` | `truediv`, `div`, `divide` |\n", "| `//` | `floordiv` |\n", "| `%` | `mod` |\n", "| `**` | `pow` |\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ufuncs: Operations Between DataFrames and Series\n", "\n", "When performing operations between a `DataFrame` and a `Series`, the index and column alignment is similarly maintained, and the result is similar to operations between a two-dimensional and one-dimensional NumPy array.\n", "Consider one common operation, where we find the difference of a two-dimensional array and one of its rows:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "array([[4, 4, 2, 0],\n", " [5, 8, 0, 8],\n", " [8, 2, 6, 1]])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = rng.integers(10, size=(3, 4))\n", "A" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 0, 0, 0],\n", " [ 1, 4, -2, 8],\n", " [ 4, -2, 4, 1]])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A - A[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "According to NumPy's broadcasting rules (see [Computation on Arrays: Broadcasting](02.05-Computation-on-arrays-broadcasting.ipynb)), subtraction between a two-dimensional array and one of its rows is applied row-wise.\n", "\n", "In Pandas, the convention similarly operates row-wise by default:" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": 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", "
QRST
00000
114-28
24-241
\n", "
" ], "text/plain": [ " Q R S T\n", "0 0 0 0 0\n", "1 1 4 -2 8\n", "2 4 -2 4 1" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.DataFrame(A, columns=['Q', 'R', 'S', 'T'])\n", "df - df.iloc[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you would instead like to operate column-wise, you can use the object methods mentioned earlier, while specifying the `axis` keyword:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": 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", "
QRST
000-2-4
1-30-80
2604-1
\n", "
" ], "text/plain": [ " Q R S T\n", "0 0 0 -2 -4\n", "1 -3 0 -8 0\n", "2 6 0 4 -1" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.subtract(df['R'], axis=0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that these `DataFrame`/`Series` operations, like the operations discussed previously, will automatically align indices between the two elements:" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "Q 4\n", "S 2\n", "Name: 0, dtype: int64" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "halfrow = df.iloc[0, ::2]\n", "halfrow" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": 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", "
QRST
00.0NaN0.0NaN
11.0NaN-2.0NaN
24.0NaN4.0NaN
\n", "
" ], "text/plain": [ " Q R S T\n", "0 0.0 NaN 0.0 NaN\n", "1 1.0 NaN -2.0 NaN\n", "2 4.0 NaN 4.0 NaN" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df - halfrow" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This preservation and alignment of indices and columns means that operations on data in Pandas will always maintain the data context, which prevents the common errors that might arise when working with heterogeneous and/or misaligned data in raw NumPy arrays." ] } ], "metadata": { "anaconda-cloud": {}, "jupytext": { "formats": "ipynb,md" }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.2" } }, "nbformat": 4, "nbformat_minor": 4 }