{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tools - pandas\n", "*The `pandas` library provides high-performance, easy-to-use data structures and data analysis tools. The main data structure is the `DataFrame`, which you can think of as an in-memory 2D table (like a spreadsheet, with column names and row labels). Many features available in Excel are available programmatically, such as creating pivot tables, computing columns based on other columns, plotting graphs, etc. You can also group rows by column value, or join tables much like in SQL. Pandas is also great at handling time series.*\n", "\n", "**Prerequisites:**\n", "* NumPy – if you are not familiar with NumPy, we recommend that you go through the [NumPy tutorial](tools_numpy.ipynb) now." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setup\n", "First, let's make sure this notebook works well in both python 2 and 3:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from __future__ import division\n", "from __future__ import print_function\n", "from __future__ import unicode_literals" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's import `pandas`. People usually import it as `pd`:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `Series` objects\n", "The `pandas` library contains these useful data structures:\n", "* `Series` objects, that we will discuss now. A `Series` object is 1D array, similar to a column in a spreadsheet (with a column name and row labels).\n", "* `DataFrame` objects. This is a 2D table, similar to a spreadsheet (with column names and row labels).\n", "* `Panel` objects. You can see a `Panel` as a dictionary of `DataFrame`s. These are less used, so we will not discuss them here." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Creating a `Series`\n", "Let's start by creating our first `Series` object!" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0 2\n", "1 -1\n", "2 3\n", "3 5\n", "dtype: int64" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = pd.Series([2,-1,3,5])\n", "s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Similar to a 1D `ndarray`\n", "`Series` objects behave much like one-dimensional NumPy `ndarray`s, and you can often pass them as parameters to NumPy functions:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0 7.389056\n", "1 0.367879\n", "2 20.085537\n", "3 148.413159\n", "dtype: float64" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "np.exp(s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Arithmetic operations on `Series` are also possible, and they apply *elementwise*, just like for `ndarray`s:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0 1002\n", "1 1999\n", "2 3003\n", "3 4005\n", "dtype: int64" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s + [1000,2000,3000,4000]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Similar to NumPy, if you add a single number to a `Series`, that number is added to all items in the `Series`. This is called * broadcasting*:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0 1002\n", "1 999\n", "2 1003\n", "3 1005\n", "dtype: int64" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s + 1000" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The same is true for all binary operations such as `*` or `/`, and even conditional operations:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0 False\n", "1 True\n", "2 False\n", "3 False\n", "dtype: bool" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s < 0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Index labels\n", "Each item in a `Series` object has a unique identifier called the *index label*. By default, it is simply the rank of the item in the `Series` (starting at `0`) but you can also set the index labels manually:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "alice 68\n", "bob 83\n", "charles 112\n", "darwin 68\n", "dtype: int64" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s2 = pd.Series([68, 83, 112, 68], index=[\"alice\", \"bob\", \"charles\", \"darwin\"])\n", "s2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can then use the `Series` just like a `dict`:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "83" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s2[\"bob\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can still access the items by integer location, like in a regular array:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "83" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s2[1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To make it clear when you are accessing by label or by integer location, it is recommended to always use the `loc` attribute when accessing by label, and the `iloc` attribute when accessing by integer location:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "83" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s2.loc[\"bob\"]" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "83" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s2.iloc[1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Slicing a `Series` also slices the index labels:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "bob 83\n", "charles 112\n", "dtype: int64" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s2.iloc[1:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This can lead to unexpected results when using the default numeric labels, so be careful:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0 1000\n", "1 1001\n", "2 1002\n", "3 1003\n", "dtype: int64" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "surprise = pd.Series([1000, 1001, 1002, 1003])\n", "surprise" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2 1002\n", "3 1003\n", "dtype: int64" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "surprise_slice = surprise[2:]\n", "surprise_slice" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Oh look! The first element has index label `2`. The element with index label `0` is absent from the slice:" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Key error: 0\n" ] } ], "source": [ "try:\n", " surprise_slice[0]\n", "except KeyError as e:\n", " print(\"Key error:\", e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But remember that you can access elements by integer location using the `iloc` attribute. This illustrates another reason why it's always better to use `loc` and `iloc` to access `Series` objects:" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1002" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "surprise_slice.iloc[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Init from `dict`\n", "You can create a `Series` object from a `dict`. The keys will be used as index labels:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "alice 68\n", "bob 83\n", "colin 86\n", "darwin 68\n", "dtype: int64" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "weights = {\"alice\": 68, \"bob\": 83, \"colin\": 86, \"darwin\": 68}\n", "s3 = pd.Series(weights)\n", "s3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can control which elements you want to include in the `Series` and in what order by explicitly specifying the desired `index`:" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "colin 86\n", "alice 68\n", "dtype: int64" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s4 = pd.Series(weights, index = [\"colin\", \"alice\"])\n", "s4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Automatic alignment\n", "When an operation involves multiple `Series` objects, `pandas` automatically aligns items by matching index labels." ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Index([u'alice', u'bob', u'charles', u'darwin'], dtype='object')\n", "Index([u'alice', u'bob', u'colin', u'darwin'], dtype='object')\n" ] }, { "data": { "text/plain": [ "alice 136\n", "bob 166\n", "charles NaN\n", "colin NaN\n", "darwin 136\n", "dtype: float64" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print(s2.keys())\n", "print(s3.keys())\n", "\n", "s2 + s3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The resulting `Series` contains the union of index labels from `s2` and `s3`. Since `\"colin\"` is missing from `s2` and `\"charles\"` is missing from `s3`, these items have a `NaN` result value. (ie. Not-a-Number means *missing*).\n", "\n", "Automatic alignment is very handy when working with data that may come from various sources with varying structure and missing items. But if you forget to set the right index labels, you can have surprising results:" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "s2 = [ 68 83 112 68]\n", "s5 = [1000 1000 1000 1000]\n" ] }, { "data": { "text/plain": [ "0 NaN\n", "1 NaN\n", "2 NaN\n", "3 NaN\n", "alice NaN\n", "bob NaN\n", "charles NaN\n", "darwin NaN\n", "dtype: float64" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s5 = pd.Series([1000,1000,1000,1000])\n", "print(\"s2 =\", s2.values)\n", "print(\"s5 =\", s5.values)\n", "\n", "s2 + s5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pandas could not align the `Series`, since their labels do not match at all, hence the full `NaN` result." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Init with a scalar\n", "You can also initialize a `Series` object using a scalar and a list of index labels: all items will be set to the scalar." ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "life 42\n", "universe 42\n", "everything 42\n", "dtype: int64" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "meaning = pd.Series(42, [\"life\", \"universe\", \"everything\"])\n", "meaning" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `Series` name\n", "A `Series` can have a `name`:" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "bob 83\n", "alice 68\n", "Name: weights, dtype: int64" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s6 = pd.Series([83, 68], index=[\"bob\", \"alice\"], name=\"weights\")\n", "s6" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plotting a `Series`\n", "Pandas makes it easy to plot `Series` data using matplotlib (for more details on matplotlib, check out the [matplotlib tutorial](tools_matplotlib.ipynb)). Just import matplotlib and call the `plot` method:" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXEAAAEACAYAAABF+UbAAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAHgpJREFUeJzt3Xl41NW9x/H3N0BRqlDrQhWLIuIuCBYUEQniBlyXqhfc\nirgVqwKu1Yu2pC6tvX3Q6tWCiqK0qChF3K/IEoGquCBoEXe0iFwqIKKCrN/7xxkkxJBMkpk585v5\nvJ4nT5PJLzPfeSyf/HLO+Z5j7o6IiCRTSewCRESk7hTiIiIJphAXEUkwhb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FoCHQA\n7nT3DsBKwp/cBcvM9gAuA3YDdgG2MbMz4lYVTV7crOQqxBcCLSt8vWvqsYKW+nNzHGFZ5uOx68my\nLsAJZvYR8BDQ3cxGR64p2z4FFrj7a6mvxxFCvZD9DPiHuy9z9/XAeOCwyDXlymIzaw5gZj8B/h25\nHiB3If4qsKeZ7ZaayT4NKIbVC/cBb7v7bbELyTZ3H+LuLd19D8J/3ynu3i92XdmU+tN6gZlt3GKi\nB4U/qfsucKiZbZXqHelB4U7mVv6L8gmgf+rzs4G8uDFLa++U+nL39WZ2CTCR8IvjXncv1P/wAKS2\n6z0TeMvM3iD86TXE3et4CJPkqUHAGDNrBHwEnBO5nqxy9zmpv7BeB9YDbwB3x60q88zsQaAU2N7M\n/gUMBW4GHjWzc4FPgD7xKtxEzT4iIgmmiU0RkQRTiIuIJJhCXEQkwRTiIiIJphAXEUkwhbiISIIp\nxEVEEkwhLiKSYP8PyFJoPGkEGt0AAAAASUVORK5CYII=\n", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "temperatures = [4.4,5.1,6.1,6.2,6.1,6.1,5.7,5.2,4.7,4.1,3.9,3.5]\n", "s7 = pd.Series(temperatures, name=\"Temperature\")\n", "s7.plot()\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are *many* options for plotting your data. It is not necessary to list them all here: if you need a particular type of plot (histograms, pie charts, etc.), just look for it in the excellent [Visualization](http://pandas.pydata.org/pandas-docs/stable/visualization.html) section of pandas' documentation, and look at the example code." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Time series\n", "Many datasets have timestamps, and pandas is awesome at manipulating such data:\n", "* it can represent periods (such as 2016Q3) and frequencies (such as \"monthly\"),\n", "* it can convert periods to actual timestamps, and *vice versa*,\n", "* it can resample data and aggregate values any way you like,\n", "* it can handle timezones.\n", "\n", "### Time range\n", "Let's start by creating a time series using `timerange`. This returns a `DatetimeIndex` containing one datetime per hour for 12 hours starting on October 29th 2016 at 5:30pm." ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "DatetimeIndex(['2016-10-29 17:30:00', '2016-10-29 18:30:00',\n", " '2016-10-29 19:30:00', '2016-10-29 20:30:00',\n", " '2016-10-29 21:30:00', '2016-10-29 22:30:00',\n", " '2016-10-29 23:30:00', '2016-10-30 00:30:00',\n", " '2016-10-30 01:30:00', '2016-10-30 02:30:00',\n", " '2016-10-30 03:30:00', '2016-10-30 04:30:00'],\n", " dtype='datetime64[ns]', freq='H')" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dates = pd.date_range('2016/10/29 5:30pm', periods=12, freq='H')\n", "dates" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This `DatetimeIndex` may be used as an index in a `Series`:" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2016-10-29 17:30:00 4.4\n", "2016-10-29 18:30:00 5.1\n", "2016-10-29 19:30:00 6.1\n", "2016-10-29 20:30:00 6.2\n", "2016-10-29 21:30:00 6.1\n", "2016-10-29 22:30:00 6.1\n", "2016-10-29 23:30:00 5.7\n", "2016-10-30 00:30:00 5.2\n", "2016-10-30 01:30:00 4.7\n", "2016-10-30 02:30:00 4.1\n", "2016-10-30 03:30:00 3.9\n", "2016-10-30 04:30:00 3.5\n", "Freq: H, dtype: float64" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "temp_series = pd.Series(temperatures, dates)\n", "temp_series" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's plot this series:" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAWcAAAFgCAYAAABnvbg1AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAG51JRE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0vm5Vz5vwebf4rf95bsYWHh9cdL5tV\nxMyZiIiObN77OV/WRgsPD6873qyseZ9Kd0FLLTw8vO54M7HmvThXvyE2Hh5e572ZWHOdOdt2ZHoA\nOS08PLzueLOycu7n/Hw3m1PbfpTt90i6wfb7bZ9Wq4WHh9cdL+tzS7nF3Yit9m5Z9/77Jf2ipNMk\nrUi6ulYLDw+vO15WK/Vf1BZP6sC6968fuq1fq4WHh9cdL6eV8weCPdu/avu45v3nS5LtcyXdV7GF\nh4fXHS+flfpfsS3+xTlW0mWSbm/eHpT0TQ1eVXN6rRYeHl53vJzWvH779i5JOyPi7jZZeHh43fFm\nbc3lPOeIuG/9E7L9xDZYeHh43fFmbRWxt4Yr3nMVDw8PbxZWzv2cf2uzmyQt1Grh4eF1x8tq5frK\n2fY3JV0iaaNfgPiWiDi1RgsPD687Xtbnlvonp1v8lPNTkn50k9u+VquFh4fXHS+nlfMr55MlfTci\n7m+ThYeH1x0vq5VrcSYiovHLufHRLtu/ZvtW2/fYvtv2/ua61EP7bBYeHl53vJxWzvOcr5R0SNJy\nRJwcEadIOre57sqKLTw8vO54+azUw/ktBukHtnNb6RYeHl53vJxWzq+cb7P9atu7166wvdv2aySl\n/vXlOS08PLzueNmsnIvzRZJOkXRNM6u5R1JP0smSXlSxhYeH1x0vm8XZGkREBZZ14yPbT7R9nu0T\nhq4/v2YLDw+vO142K/Vwfoth+askHZD0EUmrki5cd9veWi08PLzueFmt1H9RWzypGyWd2Ly/KOlL\nkn6hubyvVgsPD687Xk4r2650ko6JiG9JUkSs2l6W9EHbZ2iwo1OtFh4eXne8bFbOmfOdtpfWLjRP\n8F9LOlXSj1Rs4eHhdcfLZuXc+Og0Sf8QEQc3uO2ciLi2RgsPD687XlYr1+JMRETjN5ffIUhERFvH\n4kxEVGAszkREBcbiTERUYP8Ii2ILVDb8SSYAAAAASUVORK5CYII=\n", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "temp_series.plot(kind=\"bar\")\n", "\n", "plt.grid(True)\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Resampling\n", "Pandas let's us resample a time series very simply. Just call the `resample` method and specify a new frequency:" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2016-10-29 16:00:00 4.40\n", "2016-10-29 18:00:00 5.60\n", "2016-10-29 20:00:00 6.15\n", "2016-10-29 22:00:00 5.90\n", "2016-10-30 00:00:00 4.95\n", "2016-10-30 02:00:00 4.00\n", "2016-10-30 04:00:00 3.50\n", "Freq: 2H, dtype: float64" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "temp_series_freq_2H = temp_series.resample(\"2H\")\n", "temp_series_freq_2H" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's take a look at the result:" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAWcAAAFgCAYAAABnvbg1AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAFw9JREFUeJzt3XuQZHV5xvHnWXbBBQLIJbuWCEs0aGk0BKNQRSo24gVN\nImVVvBCNxkoZEzVapVGJqciYFMak4i1lrJgS8RKN4v0SjWBMY0SjyLIQYEGSFBeNuyosF8ULwps/\nund3bGa7T89Mz/v+Zr6fqi57ug+nvzvrvnv2zOlfOyIEAKhlXXYAAODeGM4AUBDDGQAKYjgDQEEM\nZwAoiOEMAAVNHM62j7d9me2tw/+9zfZLViIOANYqT3Ods+11kr4p6aSIuGlmVQCwxk17WuNxkv6H\nwQwAszXtcH6GpH+eRQgAYK/OpzVsb5D0f5IeGhHfXeB53gcOAFOKCC/0+DRHzk+SdOlCg3nei8zk\ndvbZZ89s3ytxo59++vM7KraPM81wPlOc0gCAFdFpONs+UIMfBn50tjkAAEla32WjiLhT0lEzbtmn\nXq+X9dLLgv5c9OdquT+zfarr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fH\nAAAAAElFTkSuQmCC\n", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "temp_series_freq_2H.plot(kind=\"bar\")\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note how the values have automatically been aggregated into 2-hour periods. If we look at the 6-8pm period, for example, we had a value of `5.1` at 6:30pm, and `6.1` at 7:30pm. After resampling, we just have one value of `5.6`, which is the mean of `5.1` and `6.1`. Computing the mean is the default behavior, but it is also possible to use a different aggregation function, for example we can decide to keep the minimum value of each period:" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2016-10-29 16:00:00 4.4\n", "2016-10-29 18:00:00 5.1\n", "2016-10-29 20:00:00 6.1\n", "2016-10-29 22:00:00 5.7\n", "2016-10-30 00:00:00 4.7\n", "2016-10-30 02:00:00 3.9\n", "2016-10-30 04:00:00 3.5\n", "Freq: 2H, dtype: float64" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "temp_series_freq_2H = temp_series.resample(\"2H\", how=np.min)\n", "temp_series_freq_2H" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Upsampling and interpolation\n", "This was an example of downsampling. We can also upsample (ie. increase the frequency), but this creates holes in our data:" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2016-10-29 17:30:00 4.4\n", "2016-10-29 17:45:00 NaN\n", "2016-10-29 18:00:00 NaN\n", "2016-10-29 18:15:00 NaN\n", "2016-10-29 18:30:00 5.1\n", "2016-10-29 18:45:00 NaN\n", "2016-10-29 19:00:00 NaN\n", "2016-10-29 19:15:00 NaN\n", "2016-10-29 19:30:00 6.1\n", "2016-10-29 19:45:00 NaN\n", "Freq: 15T, dtype: float64" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "temp_series_freq_15min = temp_series.resample(\"15Min\")\n", "temp_series_freq_15min.head(n=10) # `head` displays the top n values" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One solution is to fill the gaps by interpolating. We just call the `interpolate` method. The default is to use linear interpolation, but we can also select another method, such as cubic interpolation:" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "2016-10-29 17:30:00 4.400000\n", "2016-10-29 17:45:00 4.430644\n", "2016-10-29 18:00:00 4.582074\n", "2016-10-29 18:15:00 4.817467\n", "2016-10-29 18:30:00 5.100000\n", "2016-10-29 18:45:00 5.394958\n", "2016-10-29 19:00:00 5.676060\n", "2016-10-29 19:15:00 5.919132\n", "2016-10-29 19:30:00 6.100000\n", "2016-10-29 19:45:00 6.202023\n", "Freq: 15T, dtype: float64" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "temp_series_freq_15min = temp_series.resample(\"15Min\").interpolate(method=\"cubic\")\n", "temp_series_freq_15min.head(n=10)" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false }, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXkAAAEACAYAAABWLgY0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3Xd4FFUXwOHfTQidAEkoARJ6EVGkd0gAkdBVuiBFBFGq\n6CeICgiKICpVAeldqvQiQihSlC5NOqF3CD0hud8fswmbkEDKbia7Oe/z7MPuzOycMzvh7OzMnXuV\n1hohhBDOycXsBIQQQtiPFHkhhHBiUuSFEMKJSZEXQggnJkVeCCGcmBR5IYRwYjYp8kqpzEqpBUqp\nI0qpQ0qpCtHm11BK3VZK7bE8vrBFXCGEEM+XykbrGQWs0lo3U0qlAtLHsMxmrXUjG8UTQggRB4ku\n8kopd6Ca1ro9gNb6CRAc06KJjSWEECJ+bHG6Jj9wXSk11XIqZqJSKl0My1VSSu1TSq1UShW3QVwh\nhBAvYIsinwooDYzTWpcGHgB9oy2zG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IIJyZFXgghnJgUeSGEcGL/Bw3zZdRfiQHgAAAAAElFTkSuQmCC\n", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "temp_series.plot(label=\"Period: 1 hour\")\n", "temp_series_freq_15min.plot(label=\"Period: 15 minutes\")\n", "plt.legend()\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Timezones\n", "By default datetimes are *naive*: they are not aware of timezones, so 2016-10-30 02:30 might mean October 30th 2016 at 2:30am in Paris or in New York. We can make datetimes timezone *aware* by calling the `tz_localize` method:" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2016-10-29 17:30:00-04:00 4.4\n", "2016-10-29 18:30:00-04:00 5.1\n", "2016-10-29 19:30:00-04:00 6.1\n", "2016-10-29 20:30:00-04:00 6.2\n", "2016-10-29 21:30:00-04:00 6.1\n", "2016-10-29 22:30:00-04:00 6.1\n", "2016-10-29 23:30:00-04:00 5.7\n", "2016-10-30 00:30:00-04:00 5.2\n", "2016-10-30 01:30:00-04:00 4.7\n", "2016-10-30 02:30:00-04:00 4.1\n", "2016-10-30 03:30:00-04:00 3.9\n", "2016-10-30 04:30:00-04:00 3.5\n", "Freq: H, dtype: float64" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "temp_series_ny = temp_series.tz_localize(\"America/New_York\")\n", "temp_series_ny" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that `-04:00` is now appended to all the datetimes. This means that these datetimes refer to [UTC](https://en.wikipedia.org/wiki/Coordinated_Universal_Time) - 4 hours.\n", "\n", "We can convert these datetimes to Paris time like this:" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2016-10-29 23:30:00+02:00 4.4\n", "2016-10-30 00:30:00+02:00 5.1\n", "2016-10-30 01:30:00+02:00 6.1\n", "2016-10-30 02:30:00+02:00 6.2\n", "2016-10-30 02:30:00+01:00 6.1\n", "2016-10-30 03:30:00+01:00 6.1\n", "2016-10-30 04:30:00+01:00 5.7\n", "2016-10-30 05:30:00+01:00 5.2\n", "2016-10-30 06:30:00+01:00 4.7\n", "2016-10-30 07:30:00+01:00 4.1\n", "2016-10-30 08:30:00+01:00 3.9\n", "2016-10-30 09:30:00+01:00 3.5\n", "Freq: H, dtype: float64" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "temp_series_paris = temp_series_ny.tz_convert(\"Europe/Paris\")\n", "temp_series_paris" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You may have noticed that the UTC offset changes from `+02:00` to `+01:00`: this is because France switches to winter time at 3am that particular night (time goes back to 2am). Notice that 2:30am occurs twice! Let's go back to a naive representation (if you log some data hourly using local time, without storing the timezone, you might get something like this):" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2016-10-29 23:30:00 4.4\n", "2016-10-30 00:30:00 5.1\n", "2016-10-30 01:30:00 6.1\n", "2016-10-30 02:30:00 6.2\n", "2016-10-30 02:30:00 6.1\n", "2016-10-30 03:30:00 6.1\n", "2016-10-30 04:30:00 5.7\n", "2016-10-30 05:30:00 5.2\n", "2016-10-30 06:30:00 4.7\n", "2016-10-30 07:30:00 4.1\n", "2016-10-30 08:30:00 3.9\n", "2016-10-30 09:30:00 3.5\n", "Freq: H, dtype: float64" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "temp_series_paris_naive = temp_series_paris.tz_localize(None)\n", "temp_series_paris_naive" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now `02:30` is really ambiguous. If we try to localize these naive datetimes to the Paris timezone, we get an error:" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Cannot infer dst time from Timestamp('2016-10-30 02:30:00'), try using the 'ambiguous' argument\n" ] } ], "source": [ "try:\n", " temp_series_paris_naive.tz_localize(\"Europe/Paris\")\n", "except Exception as e:\n", " print(type(e))\n", " print(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Fortunately using the `ambiguous` argument we can tell pandas to infer the right DST (Daylight Saving Time) based on the order of the ambiguous timestamps:" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2016-10-29 23:30:00+02:00 4.4\n", "2016-10-30 00:30:00+02:00 5.1\n", "2016-10-30 01:30:00+02:00 6.1\n", "2016-10-30 02:30:00+02:00 6.2\n", "2016-10-30 02:30:00+01:00 6.1\n", "2016-10-30 03:30:00+01:00 6.1\n", "2016-10-30 04:30:00+01:00 5.7\n", "2016-10-30 05:30:00+01:00 5.2\n", "2016-10-30 06:30:00+01:00 4.7\n", "2016-10-30 07:30:00+01:00 4.1\n", "2016-10-30 08:30:00+01:00 3.9\n", "2016-10-30 09:30:00+01:00 3.5\n", "Freq: H, dtype: float64" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "temp_series_paris_naive.tz_localize(\"Europe/Paris\", ambiguous=\"infer\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Periods\n", "The `period_range` function returns a `PeriodIndex` instead of a `DatetimeIndex`. For example, let's get all quarters in 2016 and 2017:" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "PeriodIndex(['2016Q1', '2016Q2', '2016Q3', '2016Q4', '2017Q1', '2017Q2',\n", " '2017Q3', '2017Q4'],\n", " dtype='int64', freq='Q-DEC')" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "quarters = pd.period_range('2016Q1', periods=8, freq='Q')\n", "quarters" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Adding a number `N` to a `PeriodIndex` shifts the periods by `N` times the `PeriodIndex`'s frequency:" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "PeriodIndex(['2016Q4', '2017Q1', '2017Q2', '2017Q3', '2017Q4', '2018Q1',\n", " '2018Q2', '2018Q3'],\n", " dtype='int64', freq='Q-DEC')" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "quarters + 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `asfreq` method lets us change the frequency of the `PeriodIndex`. All periods are lengthened or shortened accordingly. For example, let's convert all the quarterly periods to monthly periods (zooming in):" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "PeriodIndex(['2016-03', '2016-06', '2016-09', '2016-12', '2017-03', '2017-06',\n", " '2017-09', '2017-12'],\n", " dtype='int64', freq='M')" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "quarters.asfreq(\"M\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By default, the `asfreq` zooms on the end of each period. We can tell it to zoom on the start of each period instead:" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "PeriodIndex(['2016-01', '2016-04', '2016-07', '2016-10', '2017-01', '2017-04',\n", " '2017-07', '2017-10'],\n", " dtype='int64', freq='M')" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "quarters.asfreq(\"M\", how=\"start\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And we can zoom out:" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "PeriodIndex(['2016', '2016', '2016', '2016', '2017', '2017', '2017', '2017'], dtype='int64', freq='A-DEC')" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "quarters.asfreq(\"A\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Of course we can create a `Series` with a `PeriodIndex`:" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2016Q1 300\n", "2016Q2 320\n", "2016Q3 290\n", "2016Q4 390\n", "2017Q1 320\n", "2017Q2 360\n", "2017Q3 310\n", "2017Q4 410\n", "Freq: Q-DEC, dtype: int64" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "quarterly_revenue = pd.Series([300, 320, 290, 390, 320, 360, 310, 410], index = quarters)\n", "quarterly_revenue" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "collapsed": false }, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXsAAAEMCAYAAAAlGRZyAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3X+0VfV55/H3AypxlooajBKI0RhNQIMSFZyahItGM4rB\n1tVoZum0ETtxhS41xmrEQLi2qUE7DtMm0riWtUVLVJIqcqkTBeLBNRkBG7DLcKnSjvyQFpTUaIip\ngjzzx94nbC/n99n77F+f11osz933nHO/4DnP/Z7n+zzfr7k7IiJSbMPSHoCIiCRPwV5EpAQU7EVE\nSkDBXkSkBBTsRURKQMFeRKQEWg72ZjbMzNab2dLw67vMbKOZPW9mf2dmR0TuO8vMNoXfvzCJgYuI\nSOvamdnfAGyIfP0UcKq7nwFsAmYBmNl44HJgHHARsMDMLJ7hiohIJ1oK9mY2FrgYuK96zd1XuPu+\n8MvVwNjw9nTgYXff6+6bCX4RTIptxCIi0rZWZ/bzgZuBeu22M4AnwttjgG2R720Pr4mISEqaBnsz\nmwbsdPfnAQv/RL//DWCPuz+UzBBFRKRbB7Vwn3OB6WZ2MXAocLiZPeDuv2dmXyJI75wXuf924EOR\nr8eG197DzLQpj4hIB9y9/XVQd2/5DzAFWBre/i8EC7bvH3Kf8cB64BDgROCfAavxXJ5nc+fOTXsI\nXdH40zVlypS0h9CxvP/b5338YexsK3a7e0sz+3q+Ewb05WGxzWp3n+nug2a2GBgE9gAzwwEWSl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VP+HwIPu\n/nh4eaeZHRt+/zjg1UbP4e4/B37l7o+Fl34ATIxznCJJiOP1H3muCcBwd1+fyGCldOKe2dc6mHwp\nweEGEOzd/fjQB3Hg3iADtv/km88SbAgkknVxvf4B/iua1UuM4qzGORd4hqCd19l/MPlaYDHBTm1b\ngMvd/RfhY14m2Iz/EOAXwIXu/k9mdjzByTcjCU7EudrdX4lloCIJiPP1H37vn4GL3f2lHv9VpKC0\nXYKISAmog1ZEpAQU7EVESkDBXkSkBBTsRURKQMFeRKQEFOxFREpAwV5EpAQU7EVESuD/A85b40kU\n1GywAAAAAElFTkSuQmCC\n", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "quarterly_revenue.plot(kind=\"line\")\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can convert periods to timestamps by calling `to_timestamp`. By default this will give us the first day of each period, but by setting `how` and `freq`, we can get the last hour of each period:" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2016-03-31 23:00:00 300\n", "2016-06-30 23:00:00 320\n", "2016-09-30 23:00:00 290\n", "2016-12-31 23:00:00 390\n", "2017-03-31 23:00:00 320\n", "2017-06-30 23:00:00 360\n", "2017-09-30 23:00:00 310\n", "2017-12-31 23:00:00 410\n", "Freq: Q-DEC, dtype: int64" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "last_hours = quarterly_revenue.to_timestamp(how=\"end\", freq=\"H\")\n", "last_hours" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And back to periods by calling `to_period`:" ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2016Q1 300\n", "2016Q2 320\n", "2016Q3 290\n", "2016Q4 390\n", "2017Q1 320\n", "2017Q2 360\n", "2017Q3 310\n", "2017Q4 410\n", "Freq: Q-DEC, dtype: int64" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "last_hours.to_period()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pandas also provides many other time-related functions that we recommend you check out in the [documentation](http://pandas.pydata.org/pandas-docs/stable/timeseries.html). To whet your appetite, here is one way to get the last business day of each month in 2016, at 9am:" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "PeriodIndex(['2016-01-29 09:00', '2016-02-29 09:00', '2016-03-31 09:00',\n", " '2016-04-29 09:00', '2016-05-31 09:00', '2016-06-30 09:00',\n", " '2016-07-29 09:00', '2016-08-31 09:00', '2016-09-30 09:00',\n", " '2016-10-31 09:00', '2016-11-30 09:00', '2016-12-30 09:00'],\n", " dtype='int64', freq='H')" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "months_2016 = pd.period_range(\"2016\", periods=12, freq=\"M\")\n", "one_day_after_last_days = months_2016.asfreq(\"D\") + 1\n", "last_bdays = one_day_after_last_days.to_timestamp() - pd.tseries.offsets.BDay()\n", "last_bdays.to_period(\"H\") + 9" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `DataFrame` objects\n", "A DataFrame object represents a spreadsheet, with cell values, column names and row index labels. You can define expressions to compute columns based on other columns, create pivot-tables, group rows, draw graphs, etc. You can see `DataFrame`s as dictionaries of `Series`.\n", "\n", "### Creating a `DataFrame`\n", "You can create a DataFrame by passing a dictionary of `Series` objects:" ] }, { "cell_type": "code", "execution_count": 49, "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", "
birthyearchildrenhobbyweight
alice1985NaNBiking68
bob19843Dancing83
charles19920NaN112
\n", "
" ], "text/plain": [ " birthyear children hobby weight\n", "alice 1985 NaN Biking 68\n", "bob 1984 3 Dancing 83\n", "charles 1992 0 NaN 112" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "people_dict = {\n", " \"weight\": pd.Series([68, 83, 112], index=[\"alice\", \"bob\", \"charles\"]),\n", " \"birthyear\": pd.Series([1984, 1985, 1992], index=[\"bob\", \"alice\", \"charles\"], name=\"year\"),\n", " \"children\": pd.Series([0, 3], index=[\"charles\", \"bob\"]),\n", " \"hobby\": pd.Series([\"Biking\", \"Dancing\"], index=[\"alice\", \"bob\"]),\n", "}\n", "people = pd.DataFrame(people_dict)\n", "people" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A few things to note:\n", "* the `Series` were automatically aligned based on their index,\n", "* missing values are represented as `NaN`,\n", "* `Series` names are ignored (the name `\"year\"` was dropped),\n", "* `DataFrame`s are displayed nicely in Jupyter notebooks, woohoo!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can access columns pretty much as you would expect. They are returned as `Series` objects:" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "alice 1985\n", "bob 1984\n", "charles 1992\n", "Name: birthyear, dtype: int64" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "people[\"birthyear\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also get multiple columns at once:" ] }, { "cell_type": "code", "execution_count": 51, "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", "
birthyearhobby
alice1985Biking
bob1984Dancing
charles1992NaN
\n", "
" ], "text/plain": [ " birthyear hobby\n", "alice 1985 Biking\n", "bob 1984 Dancing\n", "charles 1992 NaN" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "people[[\"birthyear\", \"hobby\"]]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you pass a list of columns and/or index row labels to the `DataFrame` constructor, it will guarantee that these columns and/or rows will exist, in that order, and no other column/row will exist. For example:" ] }, { "cell_type": "code", "execution_count": 52, "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", "
birthyearweightheight
bob198483NaN
alice198568NaN
eugeneNaNNaNNaN
\n", "
" ], "text/plain": [ " birthyear weight height\n", "bob 1984 83 NaN\n", "alice 1985 68 NaN\n", "eugene NaN NaN NaN" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d2 = pd.DataFrame(\n", " people_dict,\n", " columns=[\"birthyear\", \"weight\", \"height\"],\n", " index=[\"bob\", \"alice\", \"eugene\"]\n", " )\n", "d2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another convenient way to create a `DataFrame` is to pass all the values to the constructor as an `ndarray`, or a list of lists, and specify the column names and row index labels separately:" ] }, { "cell_type": "code", "execution_count": 53, "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", "
birthyearchildrenhobbyweight
alice1985NaNBiking68
bob19843Dancing83
charles19920NaN112
\n", "
" ], "text/plain": [ " birthyear children hobby weight\n", "alice 1985 NaN Biking 68\n", "bob 1984 3 Dancing 83\n", "charles 1992 0 NaN 112" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "values = [\n", " [1985, np.nan, \"Biking\", 68],\n", " [1984, 3, \"Dancing\", 83],\n", " [1992, 0, np.nan, 112]\n", " ]\n", "d3 = pd.DataFrame(\n", " values,\n", " columns=[\"birthyear\", \"children\", \"hobby\", \"weight\"],\n", " index=[\"alice\", \"bob\", \"charles\"]\n", " )\n", "d3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To specify missing values, you can either use `np.nan` or NumPy's masked arrays:" ] }, { "cell_type": "code", "execution_count": 54, "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", "
birthyearchildrenhobbyweight
alice1985NaNBiking68
bob19843Dancing83
charles19920NaN112
\n", "
" ], "text/plain": [ " birthyear children hobby weight\n", "alice 1985 NaN Biking 68\n", "bob 1984 3 Dancing 83\n", "charles 1992 0 NaN 112" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "masked_array = np.ma.asarray(values, dtype=np.object)\n", "masked_array[(0, 2), (1, 2)] = np.ma.masked\n", "d3 = pd.DataFrame(\n", " values,\n", " columns=[\"birthyear\", \"children\", \"hobby\", \"weight\"],\n", " index=[\"alice\", \"bob\", \"charles\"]\n", " )\n", "d3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Instead of an `ndarray`, you can also pass a `DataFrame` object:" ] }, { "cell_type": "code", "execution_count": 55, "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", "
hobbychildren
aliceBikingNaN
bobDancing3
\n", "
" ], "text/plain": [ " hobby children\n", "alice Biking NaN\n", "bob Dancing 3" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d4 = pd.DataFrame(\n", " d3,\n", " columns=[\"hobby\", \"children\"],\n", " index=[\"alice\", \"bob\"]\n", " )\n", "d4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is also possible to create a `DataFrame` with a dictionary (or list) of dictionaries (or list):" ] }, { "cell_type": "code", "execution_count": 56, "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", "
birthyearchildrenhobbyweight
alice1985NaNBiking68
bob19843Dancing83
charles19920NaN112
\n", "
" ], "text/plain": [ " birthyear children hobby weight\n", "alice 1985 NaN Biking 68\n", "bob 1984 3 Dancing 83\n", "charles 1992 0 NaN 112" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "people = pd.DataFrame({\n", " \"birthyear\": {\"alice\":1985, \"bob\": 1984, \"charles\": 1992},\n", " \"hobby\": {\"alice\":\"Biking\", \"bob\": \"Dancing\"},\n", " \"weight\": {\"alice\":68, \"bob\": 83, \"charles\": 112},\n", " \"children\": {\"bob\": 3, \"charles\": 0}\n", "})\n", "people" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Multi-indexing\n", "If all columns are tuples of the same size, then they are understood as a multi-index. The same goes for row index labels. For example:" ] }, { "cell_type": "code", "execution_count": 57, "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", " \n", "
privatepublic
childrenweightbirthyearhobby
Londoncharles01121992NaN
ParisaliceNaN681985Biking
bob3831984Dancing
\n", "
" ], "text/plain": [ " private public \n", " children weight birthyear hobby\n", "London charles 0 112 1992 NaN\n", "Paris alice NaN 68 1985 Biking\n", " bob 3 83 1984 Dancing" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d5 = pd.DataFrame(\n", " {\n", " (\"public\", \"birthyear\"):\n", " {(\"Paris\",\"alice\"):1985, (\"Paris\",\"bob\"): 1984, (\"London\",\"charles\"): 1992},\n", " (\"public\", \"hobby\"):\n", " {(\"Paris\",\"alice\"):\"Biking\", (\"Paris\",\"bob\"): \"Dancing\"},\n", " (\"private\", \"weight\"):\n", " {(\"Paris\",\"alice\"):68, (\"Paris\",\"bob\"): 83, (\"London\",\"charles\"): 112},\n", " (\"private\", \"children\"):\n", " {(\"Paris\", \"alice\"):np.nan, (\"Paris\",\"bob\"): 3, (\"London\",\"charles\"): 0}\n", " }\n", ")\n", "d5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can now get a `DataFrame` containing all the `\"public\"` columns very simply:" ] }, { "cell_type": "code", "execution_count": 58, "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", "
birthyearhobby
Londoncharles1992NaN
Parisalice1985Biking
bob1984Dancing
\n", "
" ], "text/plain": [ " birthyear hobby\n", "London charles 1992 NaN\n", "Paris alice 1985 Biking\n", " bob 1984 Dancing" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d5[\"public\"]" ] }, { "cell_type": "code", "execution_count": 59, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "London charles NaN\n", "Paris alice Biking\n", " bob Dancing\n", "Name: (public, hobby), dtype: object" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d5[\"public\", \"hobby\"] # Same result as d4[\"public\"][\"hobby\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dropping a level\n", "Let's look at `d5` again:" ] }, { "cell_type": "code", "execution_count": 60, "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", " \n", "
privatepublic
childrenweightbirthyearhobby
Londoncharles01121992NaN
ParisaliceNaN681985Biking
bob3831984Dancing
\n", "
" ], "text/plain": [ " private public \n", " children weight birthyear hobby\n", "London charles 0 112 1992 NaN\n", "Paris alice NaN 68 1985 Biking\n", " bob 3 83 1984 Dancing" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are two levels of columns, and two levels of indices. We can drop a column level by calling `droplevel` (the same goes for indices):" ] }, { "cell_type": "code", "execution_count": 61, "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", "
childrenweightbirthyearhobby
Londoncharles01121992NaN
ParisaliceNaN681985Biking
bob3831984Dancing
\n", "
" ], "text/plain": [ " children weight birthyear hobby\n", "London charles 0 112 1992 NaN\n", "Paris alice NaN 68 1985 Biking\n", " bob 3 83 1984 Dancing" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d5.columns = d5.columns.droplevel(level = 0)\n", "d5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Transposing\n", "You can swap columns and indices using the `T` attribute:" ] }, { "cell_type": "code", "execution_count": 62, "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", "
LondonParis
charlesalicebob
children0NaN3
weight1126883
birthyear199219851984
hobbyNaNBikingDancing
\n", "
" ], "text/plain": [ " London Paris \n", " charles alice bob\n", "children 0 NaN 3\n", "weight 112 68 83\n", "birthyear 1992 1985 1984\n", "hobby NaN Biking Dancing" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d6 = d5.T\n", "d6" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Stacking and unstacking levels\n", "Calling the `stack` method will push the lowest column level after the lowest index:" ] }, { "cell_type": "code", "execution_count": 63, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
LondonParis
childrenbobNaN3
charles0NaN
weightaliceNaN68
bobNaN83
charles112NaN
birthyearaliceNaN1985
bobNaN1984
charles1992NaN
hobbyaliceNaNBiking
bobNaNDancing
\n", "
" ], "text/plain": [ " London Paris\n", "children bob NaN 3\n", " charles 0 NaN\n", "weight alice NaN 68\n", " bob NaN 83\n", " charles 112 NaN\n", "birthyear alice NaN 1985\n", " bob NaN 1984\n", " charles 1992 NaN\n", "hobby alice NaN Biking\n", " bob NaN Dancing" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d7 = d6.stack()\n", "d7" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that many `NaN` values appeared. This makes sense because many new combinations did not exist before (eg. there was no `bob` in `London`).\n", "\n", "Calling `unstack` will do the reverse, once again creating many `NaN` values." ] }, { "cell_type": "code", "execution_count": 64, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
LondonParis
alicebobcharlesalicebobcharles
childrenNaNNaN0NaN3NaN
weightNaNNaN1126883NaN
birthyearNaNNaN199219851984NaN
hobbyNaNNaNNaNBikingDancingNaN
\n", "
" ], "text/plain": [ " London Paris \n", " alice bob charles alice bob charles\n", "children NaN NaN 0 NaN 3 NaN\n", "weight NaN NaN 112 68 83 NaN\n", "birthyear NaN NaN 1992 1985 1984 NaN\n", "hobby NaN NaN NaN Biking Dancing NaN" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d8 = d7.unstack()\n", "d8" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we call `unstack` again, we end up with a `Series` object:" ] }, { "cell_type": "code", "execution_count": 65, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "London alice children NaN\n", " weight NaN\n", " birthyear NaN\n", " hobby NaN\n", " bob children NaN\n", " weight NaN\n", " birthyear NaN\n", " hobby NaN\n", " charles children 0\n", " weight 112\n", " birthyear 1992\n", " hobby NaN\n", "Paris alice children NaN\n", " weight 68\n", " birthyear 1985\n", " hobby Biking\n", " bob children 3\n", " weight 83\n", " birthyear 1984\n", " hobby Dancing\n", " charles children NaN\n", " weight NaN\n", " birthyear NaN\n", " hobby NaN\n", "dtype: object" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d9 = d8.unstack()\n", "d9" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `stack` and `unstack` methods let you select the `level` to stack/unstack. You can even stack/unstack multiple levels at once:" ] }, { "cell_type": "code", "execution_count": 66, "metadata": { "collapsed": false, "scrolled": true }, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
LondonParis
alicebobcharlesalicebobcharles
childrenNaNNaN0NaN3NaN
weightNaNNaN1126883NaN
birthyearNaNNaN199219851984NaN
hobbyNaNNaNNaNBikingDancingNaN
\n", "
" ], "text/plain": [ " London Paris \n", " alice bob charles alice bob charles\n", "children NaN NaN 0 NaN 3 NaN\n", "weight NaN NaN 112 68 83 NaN\n", "birthyear NaN NaN 1992 1985 1984 NaN\n", "hobby NaN NaN NaN Biking Dancing NaN" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d10 = d9.unstack(level = (0,1))\n", "d10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Most methods return modified copies\n", "As you may have noticed, the `stack` and `unstack` methods do not modify the object they apply to. Instead, they work on a copy and return that copy. This is true of most methods in pandas." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Accessing rows\n", "Let's go back to the `people` `DataFrame`:" ] }, { "cell_type": "code", "execution_count": 67, "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", "
birthyearchildrenhobbyweight
alice1985NaNBiking68
bob19843Dancing83
charles19920NaN112
\n", "
" ], "text/plain": [ " birthyear children hobby weight\n", "alice 1985 NaN Biking 68\n", "bob 1984 3 Dancing 83\n", "charles 1992 0 NaN 112" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "people" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `loc` attribute lets you access rows instead of columns. The result is `Series` object in which the `DataFrame`'s column names are mapped to row index labels:" ] }, { "cell_type": "code", "execution_count": 68, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "birthyear 1992\n", "children 0\n", "hobby NaN\n", "weight 112\n", "Name: charles, dtype: object" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "people.loc[\"charles\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also access rows by integer location using the `iloc` attribute:" ] }, { "cell_type": "code", "execution_count": 69, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "birthyear 1992\n", "children 0\n", "hobby NaN\n", "weight 112\n", "Name: charles, dtype: object" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "people.iloc[2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also get a slice of rows, and this returns a `DataFrame` object:" ] }, { "cell_type": "code", "execution_count": 70, "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", "
birthyearchildrenhobbyweight
bob19843Dancing83
charles19920NaN112
\n", "
" ], "text/plain": [ " birthyear children hobby weight\n", "bob 1984 3 Dancing 83\n", "charles 1992 0 NaN 112" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "people.iloc[1:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, you can pass a boolean array to get the matching rows:" ] }, { "cell_type": "code", "execution_count": 71, "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", "
birthyearchildrenhobbyweight
alice1985NaNBiking68
charles19920NaN112
\n", "
" ], "text/plain": [ " birthyear children hobby weight\n", "alice 1985 NaN Biking 68\n", "charles 1992 0 NaN 112" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "people[np.array([True, False, True])]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is most useful when combined with boolean expressions:" ] }, { "cell_type": "code", "execution_count": 72, "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", "
birthyearchildrenhobbyweight
alice1985NaNBiking68
bob19843Dancing83
\n", "
" ], "text/plain": [ " birthyear children hobby weight\n", "alice 1985 NaN Biking 68\n", "bob 1984 3 Dancing 83" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "people[people[\"birthyear\"] < 1990]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Adding and removing columns\n", "You can generally treat `DataFrame` objects like dictionaries of `Series`, so the following work fine:" ] }, { "cell_type": "code", "execution_count": 73, "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", "
birthyearchildrenhobbyweight
alice1985NaNBiking68
bob19843Dancing83
charles19920NaN112
\n", "
" ], "text/plain": [ " birthyear children hobby weight\n", "alice 1985 NaN Biking 68\n", "bob 1984 3 Dancing 83\n", "charles 1992 0 NaN 112" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "people" ] }, { "cell_type": "code", "execution_count": 74, "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", "
hobbyweightageover 30
aliceBiking6831True
bobDancing8332True
charlesNaN11224False
\n", "
" ], "text/plain": [ " hobby weight age over 30\n", "alice Biking 68 31 True\n", "bob Dancing 83 32 True\n", "charles NaN 112 24 False" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "people[\"age\"] = 2016 - people[\"birthyear\"] # adds a new column \"age\"\n", "people[\"over 30\"] = people[\"age\"] > 30 # adds another column \"over 30\"\n", "birthyears = people.pop(\"birthyear\")\n", "del people[\"children\"]\n", "\n", "people" ] }, { "cell_type": "code", "execution_count": 75, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "alice 1985\n", "bob 1984\n", "charles 1992\n", "Name: birthyear, dtype: int64" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "birthyears" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When you add a new colum, it must have the same number of rows. Missing rows are filled with NaN, and extra rows are ignored:" ] }, { "cell_type": "code", "execution_count": 76, "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", "
hobbyweightageover 30pets
aliceBiking6831TrueNaN
bobDancing8332True0
charlesNaN11224False5
\n", "
" ], "text/plain": [ " hobby weight age over 30 pets\n", "alice Biking 68 31 True NaN\n", "bob Dancing 83 32 True 0\n", "charles NaN 112 24 False 5" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "people[\"pets\"] = pd.Series({\"bob\": 0, \"charles\": 5, \"eugene\":1}) # alice is missing, eugene is ignored\n", "people" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When adding a new column, it is added at the end (on the right) by default. You can also insert a column anywhere else using the `insert` method:" ] }, { "cell_type": "code", "execution_count": 77, "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", "
hobbyheightweightageover 30pets
aliceBiking1726831TrueNaN
bobDancing1818332True0
charlesNaN18511224False5
\n", "
" ], "text/plain": [ " hobby height weight age over 30 pets\n", "alice Biking 172 68 31 True NaN\n", "bob Dancing 181 83 32 True 0\n", "charles NaN 185 112 24 False 5" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "people.insert(1, \"height\", [172, 181, 185])\n", "people" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Assigning new columns\n", "You can also create new columns by calling the `assign` method. Note that this returns a new `DataFrame` object, the original is not modified:" ] }, { "cell_type": "code", "execution_count": 78, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
hobbyheightweightageover 30petsbody_mass_indexhas_pets
aliceBiking1726831TrueNaN22.985398False
bobDancing1818332True025.335002False
charlesNaN18511224False532.724617True
\n", "
" ], "text/plain": [ " hobby height weight age over 30 pets body_mass_index has_pets\n", "alice Biking 172 68 31 True NaN 22.985398 False\n", "bob Dancing 181 83 32 True 0 25.335002 False\n", "charles NaN 185 112 24 False 5 32.724617 True" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "people.assign(\n", " body_mass_index = people[\"weight\"] / (people[\"height\"] / 100) ** 2,\n", " has_pets = people[\"pets\"] > 0\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that you cannot access columns created within the same assignment:" ] }, { "cell_type": "code", "execution_count": 79, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Key error: u'body_mass_index'\n" ] } ], "source": [ "try:\n", " people.assign(\n", " body_mass_index = people[\"weight\"] / (people[\"height\"] / 100) ** 2,\n", " overweight = people[\"body_mass_index\"] > 25\n", " )\n", "except KeyError as e:\n", " print(\"Key error:\", e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The solution is to split this assignment in two consecutive assignments:" ] }, { "cell_type": "code", "execution_count": 80, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
hobbyheightweightageover 30petsbody_mass_indexoverweight
aliceBiking1726831TrueNaN22.985398False
bobDancing1818332True025.335002True
charlesNaN18511224False532.724617True
\n", "
" ], "text/plain": [ " hobby height weight age over 30 pets body_mass_index \\\n", "alice Biking 172 68 31 True NaN 22.985398 \n", "bob Dancing 181 83 32 True 0 25.335002 \n", "charles NaN 185 112 24 False 5 32.724617 \n", "\n", " overweight \n", "alice False \n", "bob True \n", "charles True " ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d6 = people.assign(body_mass_index = people[\"weight\"] / (people[\"height\"] / 100) ** 2)\n", "d6.assign(overweight = d6[\"body_mass_index\"] > 25)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Having to create a temporary variable `d6` is not very convenient. You may want to just chain the assigment calls, but it does not work because the `people` object is not actually modified by the first assignment:" ] }, { "cell_type": "code", "execution_count": 81, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Key error: u'body_mass_index'\n" ] } ], "source": [ "try:\n", " (people\n", " .assign(body_mass_index = people[\"weight\"] / (people[\"height\"] / 100) ** 2)\n", " .assign(overweight = people[\"body_mass_index\"] > 25)\n", " )\n", "except KeyError as e:\n", " print(\"Key error:\", e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But fear not, there is a simple solution. You can pass a function to the `assign` method (typically a `lambda` function), and this function will be called with the `DataFrame` as a parameter:" ] }, { "cell_type": "code", "execution_count": 82, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
hobbyheightweightageover 30petsbody_mass_indexoverweight
aliceBiking1726831TrueNaN22.985398False
bobDancing1818332True025.335002True
charlesNaN18511224False532.724617True
\n", "
" ], "text/plain": [ " hobby height weight age over 30 pets body_mass_index \\\n", "alice Biking 172 68 31 True NaN 22.985398 \n", "bob Dancing 181 83 32 True 0 25.335002 \n", "charles NaN 185 112 24 False 5 32.724617 \n", "\n", " overweight \n", "alice False \n", "bob True \n", "charles True " ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(people\n", " .assign(body_mass_index = lambda df: df[\"weight\"] / (df[\"height\"] / 100) ** 2)\n", " .assign(overweight = lambda df: df[\"body_mass_index\"] > 25)\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Problem solved!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Evaluating an expression\n", "A great feature supported by pandas is expression evaluation. This relies on the `numexpr` library which must be installed." ] }, { "cell_type": "code", "execution_count": 83, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "alice False\n", "bob True\n", "charles True\n", "dtype: bool" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "people.eval(\"weight / (height/100) ** 2 > 25\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Assignment expressions are also supported, and contrary to the `assign` method, this does not create a copy of the `DataFrame`, instead it directly modifies it:" ] }, { "cell_type": "code", "execution_count": 84, "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", " \n", " \n", " \n", " \n", "
hobbyheightweightageover 30petsbody_mass_index
aliceBiking1726831TrueNaN22.985398
bobDancing1818332True025.335002
charlesNaN18511224False532.724617
\n", "
" ], "text/plain": [ " hobby height weight age over 30 pets body_mass_index\n", "alice Biking 172 68 31 True NaN 22.985398\n", "bob Dancing 181 83 32 True 0 25.335002\n", "charles NaN 185 112 24 False 5 32.724617" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "people.eval(\"body_mass_index = weight / (height/100) ** 2\")\n", "people" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can use a local or global variable in an expression by prefixing it with `'@'`:" ] }, { "cell_type": "code", "execution_count": 85, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
hobbyheightweightageover 30petsbody_mass_indexoverweight
aliceBiking1726831TrueNaN22.985398False
bobDancing1818332True025.335002False
charlesNaN18511224False532.724617True
\n", "
" ], "text/plain": [ " hobby height weight age over 30 pets body_mass_index \\\n", "alice Biking 172 68 31 True NaN 22.985398 \n", "bob Dancing 181 83 32 True 0 25.335002 \n", "charles NaN 185 112 24 False 5 32.724617 \n", "\n", " overweight \n", "alice False \n", "bob False \n", "charles True " ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "overweight_threshold = 30\n", "people.eval(\"overweight = body_mass_index > @overweight_threshold\")\n", "people" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Querying a `DataFrame`\n", "The `query` method lets you filter a `DataFrame` based on a query expression:" ] }, { "cell_type": "code", "execution_count": 86, "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", "
hobbyheightweightageover 30petsbody_mass_indexoverweight
bobDancing1818332True025.335002False
\n", "
" ], "text/plain": [ " hobby height weight age over 30 pets body_mass_index overweight\n", "bob Dancing 181 83 32 True 0 25.335002 False" ] }, "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ "people.query(\"age > 30 and pets == 0\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Sorting a `DataFrame`\n", "You can sort a `DataFrame` by calling its `sort_index` method. By default it sorts the rows by their index label, in ascending order, but let's reverse the order:" ] }, { "cell_type": "code", "execution_count": 87, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
hobbyheightweightageover 30petsbody_mass_indexoverweight
charlesNaN18511224False532.724617True
bobDancing1818332True025.335002False
aliceBiking1726831TrueNaN22.985398False
\n", "
" ], "text/plain": [ " hobby height weight age over 30 pets body_mass_index \\\n", "charles NaN 185 112 24 False 5 32.724617 \n", "bob Dancing 181 83 32 True 0 25.335002 \n", "alice Biking 172 68 31 True NaN 22.985398 \n", "\n", " overweight \n", "charles True \n", "bob False \n", "alice False " ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "people.sort_index(ascending=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that `sort_index` returned a sorted *copy* of the `DataFrame`. To modify `people` directly, we can set the `inplace` argument to `True`. Also, we can sort the columns instead of the rows by setting `axis=1`:" ] }, { "cell_type": "code", "execution_count": 88, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
agebody_mass_indexheighthobbyover 30overweightpetsweight
alice3122.985398172BikingTrueFalseNaN68
bob3225.335002181DancingTrueFalse083
charles2432.724617185NaNFalseTrue5112
\n", "
" ], "text/plain": [ " age body_mass_index height hobby over 30 overweight pets \\\n", "alice 31 22.985398 172 Biking True False NaN \n", "bob 32 25.335002 181 Dancing True False 0 \n", "charles 24 32.724617 185 NaN False True 5 \n", "\n", " weight \n", "alice 68 \n", "bob 83 \n", "charles 112 " ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "people.sort_index(axis=1, inplace=True)\n", "people" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To sort the `DataFrame` by the values instead of the labels, we can use `sort_values` and specify the column to sort by:" ] }, { "cell_type": "code", "execution_count": 89, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
agebody_mass_indexheighthobbyover 30overweightpetsweight
charles2432.724617185NaNFalseTrue5112
alice3122.985398172BikingTrueFalseNaN68
bob3225.335002181DancingTrueFalse083
\n", "
" ], "text/plain": [ " age body_mass_index height hobby over 30 overweight pets \\\n", "charles 24 32.724617 185 NaN False True 5 \n", "alice 31 22.985398 172 Biking True False NaN \n", "bob 32 25.335002 181 Dancing True False 0 \n", "\n", " weight \n", "charles 112 \n", "alice 68 \n", "bob 83 " ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "people.sort_values(by=\"age\", inplace=True)\n", "people" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plotting a `DataFrame`\n", "Just like for `Series`, pandas makes it easy to draw nice graphs based on a `DataFrame`.\n", "\n", "For example, it is trivial to create a line plot from a `DataFrame`'s data by calling its `plot` method:" ] }, { "cell_type": "code", "execution_count": 90, "metadata": { "collapsed": false }, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAEQCAYAAACgBo8fAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3Xt0VPW99/H3l3sIhFsEhGAIGCEWtIpS7VGYar09y6J9\nekqPx8vxcqyX42U9dfUotRZ8Vvt4edrVnuOqrV1SxCpV8ZzHeynHU2KXrWhtQVESEsQQBEHCPeGS\n2/f5Y+/ESTK5TzKTnc9rrVns+c2ePd8Z4DN7fvv329vcHRER6fsGpLoAERFJDgW6iEhEKNBFRCJC\ngS4iEhEKdBGRiFCgi4hERLuBbmY5ZvYHM/vQzDaY2R1h+xgzW21mm8zs92Y2Ku45i8ys1MyKzOzC\nnnwDIiISsPbGoZvZRGCiu683sxHAX4HLgOuAPe7+sJndDYxx93vM7GTgaeBMIAd4Hch3DXgXEelR\n7e6hu/tOd18fLlcCRQRBfRmwPFxtOXB5uLwAeMbda929DCgF5ia5bhERaaZTfehmNhX4IrAWmODu\nuyAIfWB8uNpkYFvc07aHbSIi0oM6HOhhd8vzwJ3hnnrzLhR1qYiIpNCgjqxkZoMIwvw37v5i2LzL\nzCa4+66wn/2zsH07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BrwGEe+sfm9nf\nNz7BrOH15wIXAacB3zWz3C68vkSUAl16UzHBJd02Ely8+KfAdcDzZvYewZ7rYx5caOTbwGvhQdFd\nzbbzEsEl4Z5o5/XauuydA7j7+wQXDSki+JJouCh0FvBKWNcfCS7lBvB/zex9M3sf+FP4/M5qeO11\nBOeIf5/galTvxK1zFXBDeED2A2CBmQ0BfgVc58F1We8iDS8vJ6mj86FLn2PBxXh/4u5pf31Vkd6k\nPnTpU8zsbuBm0vj6ryKpoj106fPM7HsEBy6doH/aCYYzPtALr30h8BCfd+cYwQidb/T0a4s0p0AX\nEYkIHRQVEYkIBbqISEQo0EVEIkKBLiISEQp0EZGI+P8vGw3fHxPMKwAAAABJRU5ErkJggg==\n", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "people.plot(kind = \"line\", x = \"body_mass_index\", y = [\"height\", \"weight\"])\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can pass extra arguments supported by matplotlib's functions. For example, we can create scatterplot and pass it a list of sizes using the `s` argument of matplotlib's `scatter` function:" ] }, { "cell_type": "code", "execution_count": 91, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYwAAAEPCAYAAABRHfM8AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAGFVJREFUeJzt3XuUnXV97/H3N+RGEsFwSwQSBI0YoIggxCUgW7lJPQUU\niohFQJF6KD0Vag6ksBZZq7bcDkdaKloQ07Q2QjhVwC4gAs2oIIISJNyEVARCgAgCgXAZIPmeP/aT\nOBlmwm/PzL5MeL/WmsWzn8ven+zJkw/PPTITSZLeyoh2B5AkDQ8WhiSpiIUhSSpiYUiSilgYkqQi\nFoYkqUhTCyMiLo+I5RGxuMe48yPigYj4VUT8R0Rs0mParIhYUk0/qJnZJEmNafYWxhzg4F7jfgTs\nnJm7AUuAWQARsRNwFDAdOAS4JCKiyf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VvB21Dq3v\n++yxXMvWoQH+3htaj7xwT5JUpJN3SUmSOoiFIUkqYmFIkopYGJKkIhaGJKmIhSFJKmJhSH2IiO0i\n4p4G5v/z6r5R65vnuIi4uJ9psxrNKLWahSH1r/gipcz858z87iDe829KP0tqFwtD6t/IiLi0erjM\nDdUDkXaIiOurB/j8OCLW3PHz7DUPyqkeWHN31B+odH6vLZVtquUfjIhzq/nPATau5v+31v8xpTIW\nhtS/acDFmbkL8DxwJHApcEpm7kn91tXf7GO57wBfyszdgVWsu1XxAeBPgV2p379pm8ycBbycmbtn\n5rHN++NIgzOs7lYrtdjDmblm62AR9afRfQS4KiLW3NxtnQf4VDeZnJCZa27eNw/4ZI9Zbs7MldW8\n9wPb0ea7K0ulLAypf909hlcBk4Dnqi2H9Vnf8w96v+eadbDdz0yQ3pK7pKT+9f5H/AXgt9XT3uoz\nROzac4bMXAG8EBF7VqOOLvys16pnakgdy8KQ+tf7jKYEPgd8MSJ+FRH3Un8edm8nAt+OiEXUn+W9\nouD9LwXu8aC3Opm3N5eGWESMz8yXquHTgcmZeWqbY0mD5jEMaeh9sroQbyT1x7Ee39Y00hBxC0OS\nVMRjGJKkIhaGJKmIhSFJKmJhSJKKWBiSpCIWhiSpyP8HgSKWja+++IAAAAAASUVORK5CYII=\n", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "people.plot(kind = \"scatter\", x = \"height\", y = \"weight\", s=[40, 120, 200])\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Again, there are way too many options to list here: the best option is to scroll through the [Visualization](http://pandas.pydata.org/pandas-docs/stable/visualization.html) page in pandas' documentation, find the plot you are interested in and look at the example code." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Operations on `DataFrame`s\n", "Although `DataFrame`s do not try to mimick NumPy arrays, there are a few similarities. Let's create a `DataFrame` to demonstrate this:" ] }, { "cell_type": "code", "execution_count": 92, "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", "
sepoctnov
alice889
bob1099
charles482
darwin91010
\n", "
" ], "text/plain": [ " sep oct nov\n", "alice 8 8 9\n", "bob 10 9 9\n", "charles 4 8 2\n", "darwin 9 10 10" ] }, "execution_count": 92, "metadata": {}, "output_type": "execute_result" } ], "source": [ "grades_array = np.array([[8,8,9],[10,9,9],[4, 8, 2], [9, 10, 10]])\n", "grades = pd.DataFrame(grades_array, columns=[\"sep\", \"oct\", \"nov\"], index=[\"alice\",\"bob\",\"charles\",\"darwin\"])\n", "grades" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can apply NumPy mathematical functions on a `DataFrame`: the function is applied to all values:" ] }, { "cell_type": "code", "execution_count": 93, "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", "
sepoctnov
alice2.8284272.8284273.000000
bob3.1622783.0000003.000000
charles2.0000002.8284271.414214
darwin3.0000003.1622783.162278
\n", "
" ], "text/plain": [ " sep oct nov\n", "alice 2.828427 2.828427 3.000000\n", "bob 3.162278 3.000000 3.000000\n", "charles 2.000000 2.828427 1.414214\n", "darwin 3.000000 3.162278 3.162278" ] }, "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sqrt(grades)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Similarly, adding a single value to a `DataFrame` will add that value to all elements in the `DataFrame`. This is called *broadcasting*:" ] }, { "cell_type": "code", "execution_count": 94, "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", "
sepoctnov
alice9910
bob111010
charles593
darwin101111
\n", "
" ], "text/plain": [ " sep oct nov\n", "alice 9 9 10\n", "bob 11 10 10\n", "charles 5 9 3\n", "darwin 10 11 11" ] }, "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], "source": [ "grades + 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Of course, the same is true for all other binary operations, including arithmetic (`*`,`/`,`**`...) and conditional (`>`, `==`...) operations:" ] }, { "cell_type": "code", "execution_count": 95, "metadata": { "collapsed": false, "scrolled": 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", "
sepoctnov
aliceTrueTrueTrue
bobTrueTrueTrue
charlesFalseTrueFalse
darwinTrueTrueTrue
\n", "
" ], "text/plain": [ " sep oct nov\n", "alice True True True\n", "bob True True True\n", "charles False True False\n", "darwin True True True" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "grades >= 5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Aggregation operations, such as computing the `max`, the `sum` or the `mean` of a `DataFrame`, apply to each column, and you get back a `Series` object:" ] }, { "cell_type": "code", "execution_count": 96, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "sep 7.75\n", "oct 8.75\n", "nov 7.50\n", "dtype: float64" ] }, "execution_count": 96, "metadata": {}, "output_type": "execute_result" } ], "source": [ "grades.mean()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `all` method is also an aggregation operation: it checks whether all values are `True` or not. Let's see during which months all students got a grade greater than `5`:" ] }, { "cell_type": "code", "execution_count": 97, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "sep False\n", "oct True\n", "nov False\n", "dtype: bool" ] }, "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(grades > 5).all()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Most of these functions take an optional `axis` parameter which lets you specify along which axis of the `DataFrame` you want the operation executed. The default is `axis=0`, meaning that the operation is executed vertically (on each column). You can set `axis=1` to execute the operation horizontally (on each row). For example, let's find out which students had all grades greater than `5`:" ] }, { "cell_type": "code", "execution_count": 98, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "alice True\n", "bob True\n", "charles False\n", "darwin True\n", "dtype: bool" ] }, "execution_count": 98, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(grades > 5).all(axis = 1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `any` method returns `True` if any value is True. Let's see who got at least one grade 10:" ] }, { "cell_type": "code", "execution_count": 99, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "alice False\n", "bob True\n", "charles False\n", "darwin True\n", "dtype: bool" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(grades == 10).any(axis = 1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you add a `Series` object to a `DataFrame` (or execute any other binary operation), pandas attempts to broadcast the operation to all *rows* in the `DataFrame`. This only works if the `Series` has the same size as the `DataFrame`s rows. For example, let's substract the `mean` of the `DataFrame` (a `Series` object) from the `DataFrame`:" ] }, { "cell_type": "code", "execution_count": 100, "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", "
sepoctnov
alice0.25-0.751.5
bob2.250.251.5
charles-3.75-0.75-5.5
darwin1.251.252.5
\n", "
" ], "text/plain": [ " sep oct nov\n", "alice 0.25 -0.75 1.5\n", "bob 2.25 0.25 1.5\n", "charles -3.75 -0.75 -5.5\n", "darwin 1.25 1.25 2.5" ] }, "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ "grades - grades.mean() # equivalent to: grades - [7.75, 8.75, 7.50]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We substracted `7.75` from all September grades, `8.75` from October grades and `7.50` from November grades. It is equivalent to substracting this `DataFrame`:" ] }, { "cell_type": "code", "execution_count": 101, "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", "
sepoctnov
alice7.758.757.5
bob7.758.757.5
charles7.758.757.5
darwin7.758.757.5
\n", "
" ], "text/plain": [ " sep oct nov\n", "alice 7.75 8.75 7.5\n", "bob 7.75 8.75 7.5\n", "charles 7.75 8.75 7.5\n", "darwin 7.75 8.75 7.5" ] }, "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.DataFrame([[7.75, 8.75, 7.50]]*4, index=grades.index, columns=grades.columns)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you want to substract the global mean from every grade, here is one way to do it:" ] }, { "cell_type": "code", "execution_count": 102, "metadata": { "collapsed": false, "scrolled": true }, "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", "
sepoctnov
alice001
bob211
charles-40-6
darwin122
\n", "
" ], "text/plain": [ " sep oct nov\n", "alice 0 0 1\n", "bob 2 1 1\n", "charles -4 0 -6\n", "darwin 1 2 2" ] }, "execution_count": 102, "metadata": {}, "output_type": "execute_result" } ], "source": [ "grades - grades.values.mean() # substracts the global mean (8.00) from all grades" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Automatic alignment\n", "Similar to `Series`, when operating on multiple `DataFrame`s, pandas automatically aligns them by row index label, but also by column names. Let's create a `DataFrame` with bonus points for each person from October to December:" ] }, { "cell_type": "code", "execution_count": 103, "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", "
octnovdec
bob0NaN2
colinNaN10
darwin010
charles330
\n", "
" ], "text/plain": [ " oct nov dec\n", "bob 0 NaN 2\n", "colin NaN 1 0\n", "darwin 0 1 0\n", "charles 3 3 0" ] }, "execution_count": 103, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bonus_array = np.array([[0,np.nan,2],[np.nan,1,0],[0, 1, 0], [3, 3, 0]])\n", "bonus_points = pd.DataFrame(bonus_array, columns=[\"oct\", \"nov\", \"dec\"], index=[\"bob\",\"colin\", \"darwin\", \"charles\"])\n", "bonus_points" ] }, { "cell_type": "code", "execution_count": 104, "metadata": { "collapsed": false, "scrolled": true }, "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", " \n", " \n", " \n", " \n", " \n", " \n", "
decnovoctsep
aliceNaNNaNNaNNaN
bobNaNNaN9NaN
charlesNaN511NaN
colinNaNNaNNaNNaN
darwinNaN1110NaN
\n", "
" ], "text/plain": [ " dec nov oct sep\n", "alice NaN NaN NaN NaN\n", "bob NaN NaN 9 NaN\n", "charles NaN 5 11 NaN\n", "colin NaN NaN NaN NaN\n", "darwin NaN 11 10 NaN" ] }, "execution_count": 104, "metadata": {}, "output_type": "execute_result" } ], "source": [ "grades + bonus_points" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Looks like the addition worked in some cases but way too many elements are now empty. That's because when aligning the `DataFrame`s, some columns and rows were only present on one side, and thus they were considered missing on the other side (`NaN`). Then adding `NaN` to a number results in `NaN`, hence the result.\n", "\n", "### Handling missing data\n", "Dealing with missing data is a frequent task when working with real life data. Pandas offers a few tools to handle missing data.\n", " \n", "Let's try to fix the problem above. For example, we can decide that missing data should result in a zero, instead of `NaN`. We can replace all `NaN` values by a any value using the `fillna` method:" ] }, { "cell_type": "code", "execution_count": 105, "metadata": { "collapsed": false, "scrolled": true }, "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", " \n", " \n", " \n", " \n", " \n", " \n", "
decnovoctsep
alice0000
bob0090
charles05110
colin0000
darwin011100
\n", "
" ], "text/plain": [ " dec nov oct sep\n", "alice 0 0 0 0\n", "bob 0 0 9 0\n", "charles 0 5 11 0\n", "colin 0 0 0 0\n", "darwin 0 11 10 0" ] }, "execution_count": 105, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(grades + bonus_points).fillna(0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It's a bit unfair that we're setting grades to zero in September, though. Perhaps we should decide that missing grades are missing grades, but missing bonus points should be replaced by zeros:" ] }, { "cell_type": "code", "execution_count": 106, "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", " \n", " \n", " \n", " \n", " \n", " \n", "
decnovoctsep
aliceNaN988
bobNaN9910
charlesNaN5114
colinNaNNaNNaNNaN
darwinNaN11109
\n", "
" ], "text/plain": [ " dec nov oct sep\n", "alice NaN 9 8 8\n", "bob NaN 9 9 10\n", "charles NaN 5 11 4\n", "colin NaN NaN NaN NaN\n", "darwin NaN 11 10 9" ] }, "execution_count": 106, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fixed_bonus_points = bonus_points.fillna(0)\n", "fixed_bonus_points.insert(0, \"sep\", 0)\n", "fixed_bonus_points.loc[\"alice\"] = 0\n", "grades + fixed_bonus_points" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That's much better: although we made up some data, we have not been too unfair.\n", "\n", "Another way to handle missing data is to interpolate. Let's look at the `bonus_points` `DataFrame` again:" ] }, { "cell_type": "code", "execution_count": 107, "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", "
octnovdec
bob0NaN2
colinNaN10
darwin010
charles330
\n", "
" ], "text/plain": [ " oct nov dec\n", "bob 0 NaN 2\n", "colin NaN 1 0\n", "darwin 0 1 0\n", "charles 3 3 0" ] }, "execution_count": 107, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bonus_points" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's call the `interpolate` method. By default, it interpolates vertically (`axis=0`), so let's tell it to interpolate horizontally (`axis=1`)." ] }, { "cell_type": "code", "execution_count": 108, "metadata": { "collapsed": false, "scrolled": 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", "
octnovdec
bob012
colinNaN10
darwin010
charles330
\n", "
" ], "text/plain": [ " oct nov dec\n", "bob 0 1 2\n", "colin NaN 1 0\n", "darwin 0 1 0\n", "charles 3 3 0" ] }, "execution_count": 108, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bonus_points.interpolate(axis=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Bob had 0 bonus points in October, and 2 in December. When we interpolate for November, we get the mean: 1 bonus point. Colin had 1 bonus point in November, but we do not know how many bonus points he had in September, so we cannot interpolate, this is why there is still a missing value in October after interpolation. To fix this, we can set the September bonus points to 0 before interpolation." ] }, { "cell_type": "code", "execution_count": 109, "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", " \n", " \n", " \n", " \n", " \n", " \n", "
sepoctnovdec
bob00.012
colin00.510
darwin00.010
charles03.030
alice00.000
\n", "
" ], "text/plain": [ " sep oct nov dec\n", "bob 0 0.0 1 2\n", "colin 0 0.5 1 0\n", "darwin 0 0.0 1 0\n", "charles 0 3.0 3 0\n", "alice 0 0.0 0 0" ] }, "execution_count": 109, "metadata": {}, "output_type": "execute_result" } ], "source": [ "better_bonus_points = bonus_points.copy()\n", "better_bonus_points.insert(0, \"sep\", 0)\n", "better_bonus_points.loc[\"alice\"] = 0\n", "better_bonus_points = better_bonus_points.interpolate(axis=1)\n", "better_bonus_points" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Great, now we have reasonable bonus points everywhere. Let's find out the final grades:" ] }, { "cell_type": "code", "execution_count": 110, "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", " \n", " \n", " \n", " \n", " \n", " \n", "
decnovoctsep
aliceNaN988
bobNaN10910
charlesNaN5114
colinNaNNaNNaNNaN
darwinNaN11109
\n", "
" ], "text/plain": [ " dec nov oct sep\n", "alice NaN 9 8 8\n", "bob NaN 10 9 10\n", "charles NaN 5 11 4\n", "colin NaN NaN NaN NaN\n", "darwin NaN 11 10 9" ] }, "execution_count": 110, "metadata": {}, "output_type": "execute_result" } ], "source": [ "grades + better_bonus_points" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is slightly annoying that the September column ends up on the right. This is because the `DataFrame`s we are adding do not have the exact same columns (the `grades` `DataFrame` is missing the `\"dec\"` column), so to make things predictable, pandas orders the final columns alphabetically. To fix this, we can simply add the missing column before adding:" ] }, { "cell_type": "code", "execution_count": 111, "metadata": { "collapsed": false, "scrolled": true }, "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", " \n", " \n", " \n", " \n", " \n", " \n", "
sepoctnovdec
alice889NaN
bob10910NaN
charles4115NaN
colinNaNNaNNaNNaN
darwin91011NaN
\n", "
" ], "text/plain": [ " sep oct nov dec\n", "alice 8 8 9 NaN\n", "bob 10 9 10 NaN\n", "charles 4 11 5 NaN\n", "colin NaN NaN NaN NaN\n", "darwin 9 10 11 NaN" ] }, "execution_count": 111, "metadata": {}, "output_type": "execute_result" } ], "source": [ "grades[\"dec\"] = np.nan\n", "final_grades = grades + better_bonus_points\n", "final_grades" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There's not much we can do about December and Colin: it's bad enough that we are making up bonus points, but we can't reasonably make up grades (well I guess some teachers probably do). So let's call the `dropna` method to get rid of rows that are full of `NaN`s:" ] }, { "cell_type": "code", "execution_count": 112, "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", "
sepoctnovdec
alice889NaN
bob10910NaN
charles4115NaN
darwin91011NaN
\n", "
" ], "text/plain": [ " sep oct nov dec\n", "alice 8 8 9 NaN\n", "bob 10 9 10 NaN\n", "charles 4 11 5 NaN\n", "darwin 9 10 11 NaN" ] }, "execution_count": 112, "metadata": {}, "output_type": "execute_result" } ], "source": [ "final_grades_clean = final_grades.dropna(how=\"all\")\n", "final_grades_clean" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's remove columns that are full of `NaN`s by setting the `axis` argument to `1`:" ] }, { "cell_type": "code", "execution_count": 113, "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", "
sepoctnov
alice889
bob10910
charles4115
darwin91011
\n", "
" ], "text/plain": [ " sep oct nov\n", "alice 8 8 9\n", "bob 10 9 10\n", "charles 4 11 5\n", "darwin 9 10 11" ] }, "execution_count": 113, "metadata": {}, "output_type": "execute_result" } ], "source": [ "final_grades_clean = final_grades_clean.dropna(axis=1, how=\"all\")\n", "final_grades_clean" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Aggregating with `groupby`\n", "Similar to the SQL language, pandas allows grouping your data into groups to run calculations over each group.\n", "\n", "First, let's add some extra data about each person so we can group them, and let's go back to the `final_grades` `DataFrame` so we can see how `NaN` values are handled:" ] }, { "cell_type": "code", "execution_count": 114, "metadata": { "collapsed": false, "scrolled": true }, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sepoctnovdechobby
alice889NaNBiking
bob10910NaNDancing
charles4115NaNNaN
colinNaNNaNNaNNaNDancing
darwin91011NaNBiking
\n", "
" ], "text/plain": [ " sep oct nov dec hobby\n", "alice 8 8 9 NaN Biking\n", "bob 10 9 10 NaN Dancing\n", "charles 4 11 5 NaN NaN\n", "colin NaN NaN NaN NaN Dancing\n", "darwin 9 10 11 NaN Biking" ] }, "execution_count": 114, "metadata": {}, "output_type": "execute_result" } ], "source": [ "final_grades[\"hobby\"] = [\"Biking\", \"Dancing\", np.nan, \"Dancing\", \"Biking\"]\n", "final_grades" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's group data in this `DataFrame` by hobby:" ] }, { "cell_type": "code", "execution_count": 115, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 115, "metadata": {}, "output_type": "execute_result" } ], "source": [ "grouped_grades = final_grades.groupby(\"hobby\")\n", "grouped_grades" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We are ready to compute the average grade per hobby:" ] }, { "cell_type": "code", "execution_count": 116, "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", "
sepoctnovdec
hobby
Biking8.5910NaN
Dancing10.0910NaN
\n", "
" ], "text/plain": [ " sep oct nov dec\n", "hobby \n", "Biking 8.5 9 10 NaN\n", "Dancing 10.0 9 10 NaN" ] }, "execution_count": 116, "metadata": {}, "output_type": "execute_result" } ], "source": [ "grouped_grades.mean()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That was easy! Note that the `NaN` values have simply been skipped when computing the means." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Pivot tables\n", "Pandas supports spreadsheet-like [pivot tables](https://en.wikipedia.org/wiki/Pivot_table) that allow quick data summarization. To illustrate this, let's create a simple `DataFrame`:" ] }, { "cell_type": "code", "execution_count": 117, "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", "
octnovdec
bob0NaN2
colinNaN10
darwin010
charles330
\n", "
" ], "text/plain": [ " oct nov dec\n", "bob 0 NaN 2\n", "colin NaN 1 0\n", "darwin 0 1 0\n", "charles 3 3 0" ] }, "execution_count": 117, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bonus_points" ] }, { "cell_type": "code", "execution_count": 118, "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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
namemonthgradebonus
0alicesep8NaN
1aliceoct8NaN
2alicenov9NaN
3bobsep100
4boboct9NaN
5bobnov102
6charlessep43
7charlesoct113
8charlesnov50
9darwinsep90
10darwinoct101
11darwinnov110
\n", "
" ], "text/plain": [ " name month grade bonus\n", "0 alice sep 8 NaN\n", "1 alice oct 8 NaN\n", "2 alice nov 9 NaN\n", "3 bob sep 10 0\n", "4 bob oct 9 NaN\n", "5 bob nov 10 2\n", "6 charles sep 4 3\n", "7 charles oct 11 3\n", "8 charles nov 5 0\n", "9 darwin sep 9 0\n", "10 darwin oct 10 1\n", "11 darwin nov 11 0" ] }, "execution_count": 118, "metadata": {}, "output_type": "execute_result" } ], "source": [ "more_grades = final_grades_clean.stack().reset_index()\n", "more_grades.columns = [\"name\", \"month\", \"grade\"]\n", "more_grades[\"bonus\"] = [np.nan, np.nan, np.nan, 0, np.nan, 2, 3, 3, 0, 0, 1, 0]\n", "more_grades" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can call the `pivot_table` function for this `DataFrame`, asking to group by the `name` column. By default, `pivot_table` computes the `mean` of each numeric column:" ] }, { "cell_type": "code", "execution_count": 119, "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", "
bonusgrade
name
aliceNaN8.333333
bob1.0000009.666667
charles2.0000006.666667
darwin0.33333310.000000
\n", "
" ], "text/plain": [ " bonus grade\n", "name \n", "alice NaN 8.333333\n", "bob 1.000000 9.666667\n", "charles 2.000000 6.666667\n", "darwin 0.333333 10.000000" ] }, "execution_count": 119, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.pivot_table(more_grades, index=\"name\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can change the aggregation function by setting the `aggfunc` attribute, and we can also specify the list of columns whose values will be aggregated:" ] }, { "cell_type": "code", "execution_count": 120, "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", "
bonusgrade
name
aliceNaN9
bob210
charles311
darwin111
\n", "
" ], "text/plain": [ " bonus grade\n", "name \n", "alice NaN 9\n", "bob 2 10\n", "charles 3 11\n", "darwin 1 11" ] }, "execution_count": 120, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.pivot_table(more_grades, index=\"name\", values=[\"grade\",\"bonus\"], aggfunc=np.max)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also specify the `columns` to aggregate over horizontally, and request the grand totals for each row and column by setting `margins=True`:" ] }, { "cell_type": "code", "execution_count": 121, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
monthnovoctsepAll
name
alice9.008.08.008.333333
bob10.009.010.009.666667
charles5.0011.04.006.666667
darwin11.0010.09.0010.000000
All8.759.57.758.666667
\n", "
" ], "text/plain": [ "month nov oct sep All\n", "name \n", "alice 9.00 8.0 8.00 8.333333\n", "bob 10.00 9.0 10.00 9.666667\n", "charles 5.00 11.0 4.00 6.666667\n", "darwin 11.00 10.0 9.00 10.000000\n", "All 8.75 9.5 7.75 8.666667" ] }, "execution_count": 121, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.pivot_table(more_grades, index=\"name\", values=\"grade\", columns=\"month\", margins=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, we can specify multiple index or column names, and pandas will create multi-level indices:" ] }, { "cell_type": "code", "execution_count": 122, "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", " \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", " \n", " \n", " \n", " \n", "
bonusgrade
namemonth
alicenovNaN9.000000
octNaN8.000000
sepNaN8.000000
bobnov2.00010.000000
octNaN9.000000
sep0.00010.000000
charlesnov0.0005.000000
oct3.00011.000000
sep3.0004.000000
darwinnov0.00011.000000
oct1.00010.000000
sep0.0009.000000
All1.1258.666667
\n", "
" ], "text/plain": [ " bonus grade\n", "name month \n", "alice nov NaN 9.000000\n", " oct NaN 8.000000\n", " sep NaN 8.000000\n", "bob nov 2.000 10.000000\n", " oct NaN 9.000000\n", " sep 0.000 10.000000\n", "charles nov 0.000 5.000000\n", " oct 3.000 11.000000\n", " sep 3.000 4.000000\n", "darwin nov 0.000 11.000000\n", " oct 1.000 10.000000\n", " sep 0.000 9.000000\n", "All 1.125 8.666667" ] }, "execution_count": 122, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.pivot_table(more_grades, index=(\"name\", \"month\"), margins=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Overview functions\n", "When dealing with large `DataFrames`, it is useful to get a quick overview of its content. Pandas offers a few functions for this. First, let's create a large `DataFrame` with a mix of numeric values, missing values and text values. Notice how Jupyter displays only the corners of the `DataFrame`:" ] }, { "cell_type": "code", "execution_count": 123, "metadata": { "collapsed": false, "scrolled": 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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ABCsome_textDEFGHI...QRSTUVWXYZ
0NaN1144Blabla99NaN8822165143...11NaN114499NaN8822165143
1112255Blabla110NaN9933NaN154...22112255110NaN9933NaN154
2223366Blabla1211111044NaN165...332233661211111044NaN165
3334477Blabla132221215511NaN...44334477132221215511NaN
4445588Blabla143331326622NaN...55445588143331326622NaN
5556699Blabla15444143773311...6655669915444143773311
66677110Blabla16555154884422...77667711016555154884422
77788121BlablaNaN66165995533...887788121NaN66165995533
88899132BlablaNaN77NaN1106644...998899132NaN77NaN1106644
999110143Blabla1188NaN1217755...110991101431188NaN1217755
10110121154Blabla2299111328866...1211101211542299111328866
11121132165Blabla33110221439977...13212113216533110221439977
12132143NaNBlabla441213315411088...143132143NaN441213315411088
13143154NaNBlabla551324416512199...154143154NaN551324416512199
1415416511Blabla6614355NaN132110...165154165116614355NaN132110
15165NaN22Blabla7715466NaN143121...NaN165NaN227715466NaN143121
16NaNNaN33Blabla881657711154132...NaNNaNNaN33881657711154132
17NaN1144Blabla99NaN8822165143...11NaN114499NaN8822165143
18112255Blabla110NaN9933NaN154...22112255110NaN9933NaN154
19223366Blabla1211111044NaN165...332233661211111044NaN165
20334477Blabla132221215511NaN...44334477132221215511NaN
21445588Blabla143331326622NaN...55445588143331326622NaN
22556699Blabla15444143773311...6655669915444143773311
236677110Blabla16555154884422...77667711016555154884422
247788121BlablaNaN66165995533...887788121NaN66165995533
258899132BlablaNaN77NaN1106644...998899132NaN77NaN1106644
2699110143Blabla1188NaN1217755...110991101431188NaN1217755
27110121154Blabla2299111328866...1211101211542299111328866
28121132165Blabla33110221439977...13212113216533110221439977
29132143NaNBlabla441213315411088...143132143NaN441213315411088
..................................................................
99708899132BlablaNaN77NaN1106644...998899132NaN77NaN1106644
997199110143Blabla1188NaN1217755...110991101431188NaN1217755
9972110121154Blabla2299111328866...1211101211542299111328866
9973121132165Blabla33110221439977...13212113216533110221439977
9974132143NaNBlabla441213315411088...143132143NaN441213315411088
9975143154NaNBlabla551324416512199...154143154NaN551324416512199
997615416511Blabla6614355NaN132110...165154165116614355NaN132110
9977165NaN22Blabla7715466NaN143121...NaN165NaN227715466NaN143121
9978NaNNaN33Blabla881657711154132...NaNNaNNaN33881657711154132
9979NaN1144Blabla99NaN8822165143...11NaN114499NaN8822165143
9980112255Blabla110NaN9933NaN154...22112255110NaN9933NaN154
9981223366Blabla1211111044NaN165...332233661211111044NaN165
9982334477Blabla132221215511NaN...44334477132221215511NaN
9983445588Blabla143331326622NaN...55445588143331326622NaN
9984556699Blabla15444143773311...6655669915444143773311
99856677110Blabla16555154884422...77667711016555154884422
99867788121BlablaNaN66165995533...887788121NaN66165995533
99878899132BlablaNaN77NaN1106644...998899132NaN77NaN1106644
998899110143Blabla1188NaN1217755...110991101431188NaN1217755
9989110121154Blabla2299111328866...1211101211542299111328866
9990121132165Blabla33110221439977...13212113216533110221439977
9991132143NaNBlabla441213315411088...143132143NaN441213315411088
9992143154NaNBlabla551324416512199...154143154NaN551324416512199
999315416511Blabla6614355NaN132110...165154165116614355NaN132110
9994165NaN22Blabla7715466NaN143121...NaN165NaN227715466NaN143121
9995NaNNaN33Blabla881657711154132...NaNNaNNaN33881657711154132
9996NaN1144Blabla99NaN8822165143...11NaN114499NaN8822165143
9997112255Blabla110NaN9933NaN154...22112255110NaN9933NaN154
9998223366Blabla1211111044NaN165...332233661211111044NaN165
9999334477Blabla132221215511NaN...44334477132221215511NaN
\n", "

10000 rows × 27 columns

\n", "
" ], "text/plain": [ " A B C some_text D E F G H I ... Q R \\\n", "0 NaN 11 44 Blabla 99 NaN 88 22 165 143 ... 11 NaN \n", "1 11 22 55 Blabla 110 NaN 99 33 NaN 154 ... 22 11 \n", "2 22 33 66 Blabla 121 11 110 44 NaN 165 ... 33 22 \n", "3 33 44 77 Blabla 132 22 121 55 11 NaN ... 44 33 \n", "4 44 55 88 Blabla 143 33 132 66 22 NaN ... 55 44 \n", "5 55 66 99 Blabla 154 44 143 77 33 11 ... 66 55 \n", "6 66 77 110 Blabla 165 55 154 88 44 22 ... 77 66 \n", "7 77 88 121 Blabla NaN 66 165 99 55 33 ... 88 77 \n", "8 88 99 132 Blabla NaN 77 NaN 110 66 44 ... 99 88 \n", "9 99 110 143 Blabla 11 88 NaN 121 77 55 ... 110 99 \n", "10 110 121 154 Blabla 22 99 11 132 88 66 ... 121 110 \n", "11 121 132 165 Blabla 33 110 22 143 99 77 ... 132 121 \n", "12 132 143 NaN Blabla 44 121 33 154 110 88 ... 143 132 \n", "13 143 154 NaN Blabla 55 132 44 165 121 99 ... 154 143 \n", "14 154 165 11 Blabla 66 143 55 NaN 132 110 ... 165 154 \n", "15 165 NaN 22 Blabla 77 154 66 NaN 143 121 ... NaN 165 \n", "16 NaN NaN 33 Blabla 88 165 77 11 154 132 ... NaN NaN \n", "17 NaN 11 44 Blabla 99 NaN 88 22 165 143 ... 11 NaN \n", "18 11 22 55 Blabla 110 NaN 99 33 NaN 154 ... 22 11 \n", "19 22 33 66 Blabla 121 11 110 44 NaN 165 ... 33 22 \n", "20 33 44 77 Blabla 132 22 121 55 11 NaN ... 44 33 \n", "21 44 55 88 Blabla 143 33 132 66 22 NaN ... 55 44 \n", "22 55 66 99 Blabla 154 44 143 77 33 11 ... 66 55 \n", "23 66 77 110 Blabla 165 55 154 88 44 22 ... 77 66 \n", "24 77 88 121 Blabla NaN 66 165 99 55 33 ... 88 77 \n", "25 88 99 132 Blabla NaN 77 NaN 110 66 44 ... 99 88 \n", "26 99 110 143 Blabla 11 88 NaN 121 77 55 ... 110 99 \n", "27 110 121 154 Blabla 22 99 11 132 88 66 ... 121 110 \n", "28 121 132 165 Blabla 33 110 22 143 99 77 ... 132 121 \n", "29 132 143 NaN Blabla 44 121 33 154 110 88 ... 143 132 \n", "... ... ... ... ... ... ... ... ... ... ... ... ... ... \n", "9970 88 99 132 Blabla NaN 77 NaN 110 66 44 ... 99 88 \n", "9971 99 110 143 Blabla 11 88 NaN 121 77 55 ... 110 99 \n", "9972 110 121 154 Blabla 22 99 11 132 88 66 ... 121 110 \n", "9973 121 132 165 Blabla 33 110 22 143 99 77 ... 132 121 \n", "9974 132 143 NaN Blabla 44 121 33 154 110 88 ... 143 132 \n", "9975 143 154 NaN Blabla 55 132 44 165 121 99 ... 154 143 \n", "9976 154 165 11 Blabla 66 143 55 NaN 132 110 ... 165 154 \n", "9977 165 NaN 22 Blabla 77 154 66 NaN 143 121 ... NaN 165 \n", "9978 NaN NaN 33 Blabla 88 165 77 11 154 132 ... NaN NaN \n", "9979 NaN 11 44 Blabla 99 NaN 88 22 165 143 ... 11 NaN \n", "9980 11 22 55 Blabla 110 NaN 99 33 NaN 154 ... 22 11 \n", "9981 22 33 66 Blabla 121 11 110 44 NaN 165 ... 33 22 \n", "9982 33 44 77 Blabla 132 22 121 55 11 NaN ... 44 33 \n", "9983 44 55 88 Blabla 143 33 132 66 22 NaN ... 55 44 \n", "9984 55 66 99 Blabla 154 44 143 77 33 11 ... 66 55 \n", "9985 66 77 110 Blabla 165 55 154 88 44 22 ... 77 66 \n", "9986 77 88 121 Blabla NaN 66 165 99 55 33 ... 88 77 \n", "9987 88 99 132 Blabla NaN 77 NaN 110 66 44 ... 99 88 \n", "9988 99 110 143 Blabla 11 88 NaN 121 77 55 ... 110 99 \n", "9989 110 121 154 Blabla 22 99 11 132 88 66 ... 121 110 \n", "9990 121 132 165 Blabla 33 110 22 143 99 77 ... 132 121 \n", "9991 132 143 NaN Blabla 44 121 33 154 110 88 ... 143 132 \n", "9992 143 154 NaN Blabla 55 132 44 165 121 99 ... 154 143 \n", "9993 154 165 11 Blabla 66 143 55 NaN 132 110 ... 165 154 \n", "9994 165 NaN 22 Blabla 77 154 66 NaN 143 121 ... NaN 165 \n", "9995 NaN NaN 33 Blabla 88 165 77 11 154 132 ... NaN NaN \n", "9996 NaN 11 44 Blabla 99 NaN 88 22 165 143 ... 11 NaN \n", "9997 11 22 55 Blabla 110 NaN 99 33 NaN 154 ... 22 11 \n", "9998 22 33 66 Blabla 121 11 110 44 NaN 165 ... 33 22 \n", "9999 33 44 77 Blabla 132 22 121 55 11 NaN ... 44 33 \n", "\n", " S T U V W X Y Z \n", "0 11 44 99 NaN 88 22 165 143 \n", "1 22 55 110 NaN 99 33 NaN 154 \n", "2 33 66 121 11 110 44 NaN 165 \n", "3 44 77 132 22 121 55 11 NaN \n", "4 55 88 143 33 132 66 22 NaN \n", "5 66 99 154 44 143 77 33 11 \n", "6 77 110 165 55 154 88 44 22 \n", "7 88 121 NaN 66 165 99 55 33 \n", "8 99 132 NaN 77 NaN 110 66 44 \n", "9 110 143 11 88 NaN 121 77 55 \n", "10 121 154 22 99 11 132 88 66 \n", "11 132 165 33 110 22 143 99 77 \n", "12 143 NaN 44 121 33 154 110 88 \n", "13 154 NaN 55 132 44 165 121 99 \n", "14 165 11 66 143 55 NaN 132 110 \n", "15 NaN 22 77 154 66 NaN 143 121 \n", "16 NaN 33 88 165 77 11 154 132 \n", "17 11 44 99 NaN 88 22 165 143 \n", "18 22 55 110 NaN 99 33 NaN 154 \n", "19 33 66 121 11 110 44 NaN 165 \n", "20 44 77 132 22 121 55 11 NaN \n", "21 55 88 143 33 132 66 22 NaN \n", "22 66 99 154 44 143 77 33 11 \n", "23 77 110 165 55 154 88 44 22 \n", "24 88 121 NaN 66 165 99 55 33 \n", "25 99 132 NaN 77 NaN 110 66 44 \n", "26 110 143 11 88 NaN 121 77 55 \n", "27 121 154 22 99 11 132 88 66 \n", "28 132 165 33 110 22 143 99 77 \n", "29 143 NaN 44 121 33 154 110 88 \n", "... ... ... ... ... ... ... ... ... \n", "9970 99 132 NaN 77 NaN 110 66 44 \n", "9971 110 143 11 88 NaN 121 77 55 \n", "9972 121 154 22 99 11 132 88 66 \n", "9973 132 165 33 110 22 143 99 77 \n", "9974 143 NaN 44 121 33 154 110 88 \n", "9975 154 NaN 55 132 44 165 121 99 \n", "9976 165 11 66 143 55 NaN 132 110 \n", "9977 NaN 22 77 154 66 NaN 143 121 \n", "9978 NaN 33 88 165 77 11 154 132 \n", "9979 11 44 99 NaN 88 22 165 143 \n", "9980 22 55 110 NaN 99 33 NaN 154 \n", "9981 33 66 121 11 110 44 NaN 165 \n", "9982 44 77 132 22 121 55 11 NaN \n", "9983 55 88 143 33 132 66 22 NaN \n", "9984 66 99 154 44 143 77 33 11 \n", "9985 77 110 165 55 154 88 44 22 \n", "9986 88 121 NaN 66 165 99 55 33 \n", "9987 99 132 NaN 77 NaN 110 66 44 \n", "9988 110 143 11 88 NaN 121 77 55 \n", "9989 121 154 22 99 11 132 88 66 \n", "9990 132 165 33 110 22 143 99 77 \n", "9991 143 NaN 44 121 33 154 110 88 \n", "9992 154 NaN 55 132 44 165 121 99 \n", "9993 165 11 66 143 55 NaN 132 110 \n", "9994 NaN 22 77 154 66 NaN 143 121 \n", "9995 NaN 33 88 165 77 11 154 132 \n", "9996 11 44 99 NaN 88 22 165 143 \n", "9997 22 55 110 NaN 99 33 NaN 154 \n", "9998 33 66 121 11 110 44 NaN 165 \n", "9999 44 77 132 22 121 55 11 NaN \n", "\n", "[10000 rows x 27 columns]" ] }, "execution_count": 123, "metadata": {}, "output_type": "execute_result" } ], "source": [ "much_data = np.fromfunction(lambda x,y: (x+y*y)%17*11, (10000, 26))\n", "large_df = pd.DataFrame(much_data, columns=list(\"ABCDEFGHIJKLMNOPQRSTUVWXYZ\"))\n", "large_df[large_df % 16 == 0] = np.nan\n", "large_df.insert(3,\"some_text\", \"Blabla\")\n", "large_df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `head` method returns the top 5 rows:" ] }, { "cell_type": "code", "execution_count": 124, "metadata": { "collapsed": false, "scrolled": 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", " \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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ABCsome_textDEFGHI...QRSTUVWXYZ
0NaN1144Blabla99NaN8822165143...11NaN114499NaN8822165143
1112255Blabla110NaN9933NaN154...22112255110NaN9933NaN154
2223366Blabla1211111044NaN165...332233661211111044NaN165
3334477Blabla132221215511NaN...44334477132221215511NaN
4445588Blabla143331326622NaN...55445588143331326622NaN
\n", "

5 rows × 27 columns

\n", "
" ], "text/plain": [ " A B C some_text D E F G H I ... Q R S T U \\\n", "0 NaN 11 44 Blabla 99 NaN 88 22 165 143 ... 11 NaN 11 44 99 \n", "1 11 22 55 Blabla 110 NaN 99 33 NaN 154 ... 22 11 22 55 110 \n", "2 22 33 66 Blabla 121 11 110 44 NaN 165 ... 33 22 33 66 121 \n", "3 33 44 77 Blabla 132 22 121 55 11 NaN ... 44 33 44 77 132 \n", "4 44 55 88 Blabla 143 33 132 66 22 NaN ... 55 44 55 88 143 \n", "\n", " V W X Y Z \n", "0 NaN 88 22 165 143 \n", "1 NaN 99 33 NaN 154 \n", "2 11 110 44 NaN 165 \n", "3 22 121 55 11 NaN \n", "4 33 132 66 22 NaN \n", "\n", "[5 rows x 27 columns]" ] }, "execution_count": 124, "metadata": {}, "output_type": "execute_result" } ], "source": [ "large_df.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Of course there's also a `tail` function to view the bottom 5 rows. You can pass the number of rows you want:" ] }, { "cell_type": "code", "execution_count": 125, "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", " \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", "
ABCsome_textDEFGHI...QRSTUVWXYZ
9998223366Blabla1211111044NaN165...332233661211111044NaN165
9999334477Blabla132221215511NaN...44334477132221215511NaN
\n", "

2 rows × 27 columns

\n", "
" ], "text/plain": [ " A B C some_text D E F G H I ... Q R S T \\\n", "9998 22 33 66 Blabla 121 11 110 44 NaN 165 ... 33 22 33 66 \n", "9999 33 44 77 Blabla 132 22 121 55 11 NaN ... 44 33 44 77 \n", "\n", " U V W X Y Z \n", "9998 121 11 110 44 NaN 165 \n", "9999 132 22 121 55 11 NaN \n", "\n", "[2 rows x 27 columns]" ] }, "execution_count": 125, "metadata": {}, "output_type": "execute_result" } ], "source": [ "large_df.tail(n=2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `info` method prints out a summary of each columns contents:" ] }, { "cell_type": "code", "execution_count": 126, "metadata": { "collapsed": false, "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Int64Index: 10000 entries, 0 to 9999\n", "Data columns (total 27 columns):\n", "A 8823 non-null float64\n", "B 8824 non-null float64\n", "C 8824 non-null float64\n", "some_text 10000 non-null object\n", "D 8824 non-null float64\n", "E 8822 non-null float64\n", "F 8824 non-null float64\n", "G 8824 non-null float64\n", "H 8822 non-null float64\n", "I 8823 non-null float64\n", "J 8823 non-null float64\n", "K 8822 non-null float64\n", "L 8824 non-null float64\n", "M 8824 non-null float64\n", "N 8822 non-null float64\n", "O 8824 non-null float64\n", "P 8824 non-null float64\n", "Q 8824 non-null float64\n", "R 8823 non-null float64\n", "S 8824 non-null float64\n", "T 8824 non-null float64\n", "U 8824 non-null float64\n", "V 8822 non-null float64\n", "W 8824 non-null float64\n", "X 8824 non-null float64\n", "Y 8822 non-null float64\n", "Z 8823 non-null float64\n", "dtypes: float64(26), object(1)\n", "memory usage: 2.1+ MB\n" ] } ], "source": [ "large_df.info()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, the `describe` method gives a nice overview of the main aggregated values over each column:\n", "* `count`: number of non-null (not NaN) values\n", "* `mean`: mean of non-null values\n", "* `std`: [standard deviation](https://en.wikipedia.org/wiki/Standard_deviation) of non-null values\n", "* `min`: minimum of non-null values\n", "* `25%`, `50%`, `75%`: 25th, 50th and 75th [percentile](https://en.wikipedia.org/wiki/Percentile) of non-null values\n", "* `max`: maximum of non-null values" ] }, { "cell_type": "code", "execution_count": 127, "metadata": { "collapsed": false, "scrolled": 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", " \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", " \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", " \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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ABCDEFGHIJ...QRSTUVWXYZ
count8823.0000008824.0000008824.0000008824.0000008822.0000008824.0000008824.0000008822.0000008823.0000008823.000000...8824.0000008823.0000008824.0000008824.0000008824.0000008822.0000008824.0000008824.0000008822.0000008823.000000
mean87.97755987.97257587.98753488.01246687.98379188.00748087.97756188.00000088.02244188.022441...87.97257587.97755987.97257587.98753488.01246687.98379188.00748087.97756188.00000088.022441
std47.53591147.53552347.52167947.52167947.53500147.51937147.52975547.53687947.53591147.535911...47.53552347.53591147.53552347.52167947.52167947.53500147.51937147.52975547.53687947.535911
min11.00000011.00000011.00000011.00000011.00000011.00000011.00000011.00000011.00000011.000000...11.00000011.00000011.00000011.00000011.00000011.00000011.00000011.00000011.00000011.000000
25%44.00000044.00000044.00000044.00000044.00000044.00000044.00000044.00000044.00000044.000000...44.00000044.00000044.00000044.00000044.00000044.00000044.00000044.00000044.00000044.000000
50%88.00000088.00000088.00000088.00000088.00000088.00000088.00000088.00000088.00000088.000000...88.00000088.00000088.00000088.00000088.00000088.00000088.00000088.00000088.00000088.000000
75%132.000000132.000000132.000000132.000000132.000000132.000000132.000000132.000000132.000000132.000000...132.000000132.000000132.000000132.000000132.000000132.000000132.000000132.000000132.000000132.000000
max165.000000165.000000165.000000165.000000165.000000165.000000165.000000165.000000165.000000165.000000...165.000000165.000000165.000000165.000000165.000000165.000000165.000000165.000000165.000000165.000000
\n", "

8 rows × 26 columns

\n", "
" ], "text/plain": [ " A B C D E \\\n", "count 8823.000000 8824.000000 8824.000000 8824.000000 8822.000000 \n", "mean 87.977559 87.972575 87.987534 88.012466 87.983791 \n", "std 47.535911 47.535523 47.521679 47.521679 47.535001 \n", "min 11.000000 11.000000 11.000000 11.000000 11.000000 \n", "25% 44.000000 44.000000 44.000000 44.000000 44.000000 \n", "50% 88.000000 88.000000 88.000000 88.000000 88.000000 \n", "75% 132.000000 132.000000 132.000000 132.000000 132.000000 \n", "max 165.000000 165.000000 165.000000 165.000000 165.000000 \n", "\n", " F G H I J \\\n", "count 8824.000000 8824.000000 8822.000000 8823.000000 8823.000000 \n", "mean 88.007480 87.977561 88.000000 88.022441 88.022441 \n", "std 47.519371 47.529755 47.536879 47.535911 47.535911 \n", "min 11.000000 11.000000 11.000000 11.000000 11.000000 \n", "25% 44.000000 44.000000 44.000000 44.000000 44.000000 \n", "50% 88.000000 88.000000 88.000000 88.000000 88.000000 \n", "75% 132.000000 132.000000 132.000000 132.000000 132.000000 \n", "max 165.000000 165.000000 165.000000 165.000000 165.000000 \n", "\n", " ... Q R S T \\\n", "count ... 8824.000000 8823.000000 8824.000000 8824.000000 \n", "mean ... 87.972575 87.977559 87.972575 87.987534 \n", "std ... 47.535523 47.535911 47.535523 47.521679 \n", "min ... 11.000000 11.000000 11.000000 11.000000 \n", "25% ... 44.000000 44.000000 44.000000 44.000000 \n", "50% ... 88.000000 88.000000 88.000000 88.000000 \n", "75% ... 132.000000 132.000000 132.000000 132.000000 \n", "max ... 165.000000 165.000000 165.000000 165.000000 \n", "\n", " U V W X Y \\\n", "count 8824.000000 8822.000000 8824.000000 8824.000000 8822.000000 \n", "mean 88.012466 87.983791 88.007480 87.977561 88.000000 \n", "std 47.521679 47.535001 47.519371 47.529755 47.536879 \n", "min 11.000000 11.000000 11.000000 11.000000 11.000000 \n", "25% 44.000000 44.000000 44.000000 44.000000 44.000000 \n", "50% 88.000000 88.000000 88.000000 88.000000 88.000000 \n", "75% 132.000000 132.000000 132.000000 132.000000 132.000000 \n", "max 165.000000 165.000000 165.000000 165.000000 165.000000 \n", "\n", " Z \n", "count 8823.000000 \n", "mean 88.022441 \n", "std 47.535911 \n", "min 11.000000 \n", "25% 44.000000 \n", "50% 88.000000 \n", "75% 132.000000 \n", "max 165.000000 \n", "\n", "[8 rows x 26 columns]" ] }, "execution_count": 127, "metadata": {}, "output_type": "execute_result" } ], "source": [ "large_df.describe()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Saving & loading\n", "Pandas can save `DataFrame`s to various backends, including file formats such as CSV, Excel, JSON, HTML and HDF5, or to a SQL database. Let's create a `DataFrame` to demonstrate this:" ] }, { "cell_type": "code", "execution_count": 128, "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", "
hobbyweightbirthyearchildren
aliceBiking68.51985NaN
bobDancing83.119843
\n", "
" ], "text/plain": [ " hobby weight birthyear children\n", "alice Biking 68.5 1985 NaN\n", "bob Dancing 83.1 1984 3" ] }, "execution_count": 128, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_df = pd.DataFrame(\n", " [[\"Biking\", 68.5, 1985, np.nan], [\"Dancing\", 83.1, 1984, 3]], \n", " columns=[\"hobby\",\"weight\",\"birthyear\",\"children\"],\n", " index=[\"alice\", \"bob\"]\n", ")\n", "my_df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Saving\n", "Let's save it to CSV, HTML and JSON:" ] }, { "cell_type": "code", "execution_count": 129, "metadata": { "collapsed": true }, "outputs": [], "source": [ "my_df.to_csv(\"my_df.csv\")\n", "my_df.to_html(\"my_df.html\")\n", "my_df.to_json(\"my_df.json\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Done! Let's take a peek at what was saved:" ] }, { "cell_type": "code", "execution_count": 130, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "# my_df.csv\n", ",hobby,weight,birthyear,children\n", "alice,Biking,68.5,1985,\n", "bob,Dancing,83.1,1984,3.0\n", "\n", "\n", "# my_df.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", "
hobbyweightbirthyearchildren
aliceBiking68.51985NaN
bobDancing83.119843
\n", "\n", "# my_df.json\n", "{\"hobby\":{\"alice\":\"Biking\",\"bob\":\"Dancing\"},\"weight\":{\"alice\":68.5,\"bob\":83.1},\"birthyear\":{\"alice\":1985,\"bob\":1984},\"children\":{\"alice\":null,\"bob\":3.0}}\n", "\n" ] } ], "source": [ "for filename in (\"my_df.csv\", \"my_df.html\", \"my_df.json\"):\n", " print(\"#\", filename)\n", " with open(filename, \"rt\") as f:\n", " print(f.read())\n", " print()\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that the index is saved as the first column (with no name) in a CSV file, as `` tags in HTML and as keys in JSON.\n", "\n", "Saving to other formats works very similarly, but some formats require extra libraries to be installed. For example, saving to Excel requires the openpyxl library:" ] }, { "cell_type": "code", "execution_count": 131, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "No module named openpyxl\n" ] } ], "source": [ "try:\n", " my_df.to_excel(\"my_df.xlsx\", sheet_name='People')\n", "except ImportError as e:\n", " print(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Loading\n", "Now let's load our CSV file back into a `DataFrame`:" ] }, { "cell_type": "code", "execution_count": 132, "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", "
hobbyweightbirthyearchildren
aliceBiking68.51985NaN
bobDancing83.119843
\n", "
" ], "text/plain": [ " hobby weight birthyear children\n", "alice Biking 68.5 1985 NaN\n", "bob Dancing 83.1 1984 3" ] }, "execution_count": 132, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_df_loaded = pd.read_csv(\"my_df.csv\", index_col=0)\n", "my_df_loaded" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As you might guess, there are similar `read_json`, `read_html`, `read_excel` functions as well. We can also read data straight from the Internet. For example, let's load all U.S. cities from [simplemaps.com](http://simplemaps.com/):" ] }, { "cell_type": "code", "execution_count": 133, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
statecitylatlng
zip
35004ALAcmar33.584132-86.515570
35005ALAdamsville33.588437-86.959727
35006ALAdger33.434277-87.167455
35007ALKeystone33.236868-86.812861
35010ALNew Site32.941445-85.951086
\n", "
" ], "text/plain": [ " state city lat lng\n", "zip \n", "35004 AL Acmar 33.584132 -86.515570\n", "35005 AL Adamsville 33.588437 -86.959727\n", "35006 AL Adger 33.434277 -87.167455\n", "35007 AL Keystone 33.236868 -86.812861\n", "35010 AL New Site 32.941445 -85.951086" ] }, "execution_count": 133, "metadata": {}, "output_type": "execute_result" } ], "source": [ "us_cities = None\n", "try:\n", " csv_url = \"http://simplemaps.com/files/cities.csv\"\n", " us_cities = pd.read_csv(csv_url, index_col=0)\n", " us_cities = us_cities.head()\n", "except IOError as e:\n", " print(e)\n", "us_cities" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are more options available, in particular regarding datetime format. Check out the [documentation](http://pandas.pydata.org/pandas-docs/stable/io.html) for more details." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Combining `DataFrame`s\n", "\n", "### SQL-like joins\n", "One powerful feature of pandas is it's ability to perform SQL-like joins on `DataFrame`s. Various types of joins are supported: inner joins, left/right outer joins and full joins. To illustrate this, let's start by creating a couple simple `DataFrame`s:" ] }, { "cell_type": "code", "execution_count": 134, "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", " \n", " \n", " \n", " \n", " \n", " \n", "
statecitylatlng
0CASan Francisco37.781334-122.416728
1NYNew York40.705649-74.008344
2FLMiami25.791100-80.320733
3OHCleveland41.473508-81.739791
4UTSalt Lake City40.755851-111.896657
\n", "
" ], "text/plain": [ " state city lat lng\n", "0 CA San Francisco 37.781334 -122.416728\n", "1 NY New York 40.705649 -74.008344\n", "2 FL Miami 25.791100 -80.320733\n", "3 OH Cleveland 41.473508 -81.739791\n", "4 UT Salt Lake City 40.755851 -111.896657" ] }, "execution_count": 134, "metadata": {}, "output_type": "execute_result" } ], "source": [ "city_loc = pd.DataFrame(\n", " [\n", " [\"CA\", \"San Francisco\", 37.781334, -122.416728],\n", " [\"NY\", \"New York\", 40.705649, -74.008344],\n", " [\"FL\", \"Miami\", 25.791100, -80.320733],\n", " [\"OH\", \"Cleveland\", 41.473508, -81.739791],\n", " [\"UT\", \"Salt Lake City\", 40.755851, -111.896657]\n", " ], columns=[\"state\", \"city\", \"lat\", \"lng\"])\n", "city_loc" ] }, { "cell_type": "code", "execution_count": 135, "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", "
populationcitystate
3808976San FranciscoCalifornia
48363710New YorkNew-York
5413201MiamiFlorida
62242193HoustonTexas
\n", "
" ], "text/plain": [ " population city state\n", "3 808976 San Francisco California\n", "4 8363710 New York New-York\n", "5 413201 Miami Florida\n", "6 2242193 Houston Texas" ] }, "execution_count": 135, "metadata": {}, "output_type": "execute_result" } ], "source": [ "city_pop = pd.DataFrame(\n", " [\n", " [808976, \"San Francisco\", \"California\"],\n", " [8363710, \"New York\", \"New-York\"],\n", " [413201, \"Miami\", \"Florida\"],\n", " [2242193, \"Houston\", \"Texas\"]\n", " ], index=[3,4,5,6], columns=[\"population\", \"city\", \"state\"])\n", "city_pop" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's join these `DataFrame`s using the `merge` function:" ] }, { "cell_type": "code", "execution_count": 136, "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", "
state_xcitylatlngpopulationstate_y
0CASan Francisco37.781334-122.416728808976California
1NYNew York40.705649-74.0083448363710New-York
2FLMiami25.791100-80.320733413201Florida
\n", "
" ], "text/plain": [ " state_x city lat lng population state_y\n", "0 CA San Francisco 37.781334 -122.416728 808976 California\n", "1 NY New York 40.705649 -74.008344 8363710 New-York\n", "2 FL Miami 25.791100 -80.320733 413201 Florida" ] }, "execution_count": 136, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.merge(left=city_loc, right=city_pop, on=\"city\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that both `DataFrame`s have a column named `state`, so in the result they got renamed to `state_x` and `state_y`.\n", "\n", "Also, note that Cleveland, Salt Lake City and Houston were dropped because they don't exist in *both* `DataFrame`s. This is the equivalent of a SQL `INNER JOIN`. If you want a `FULL OUTER JOIN`, where no city gets dropped and `NaN` values are added, you must specify `how=\"outer\"`:" ] }, { "cell_type": "code", "execution_count": 137, "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", " \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", "
state_xcitylatlngpopulationstate_y
0CASan Francisco37.781334-122.416728808976California
1NYNew York40.705649-74.0083448363710New-York
2FLMiami25.791100-80.320733413201Florida
3OHCleveland41.473508-81.739791NaNNaN
4UTSalt Lake City40.755851-111.896657NaNNaN
5NaNHoustonNaNNaN2242193Texas
\n", "
" ], "text/plain": [ " state_x city lat lng population state_y\n", "0 CA San Francisco 37.781334 -122.416728 808976 California\n", "1 NY New York 40.705649 -74.008344 8363710 New-York\n", "2 FL Miami 25.791100 -80.320733 413201 Florida\n", "3 OH Cleveland 41.473508 -81.739791 NaN NaN\n", "4 UT Salt Lake City 40.755851 -111.896657 NaN NaN\n", "5 NaN Houston NaN NaN 2242193 Texas" ] }, "execution_count": 137, "metadata": {}, "output_type": "execute_result" } ], "source": [ "all_cities = pd.merge(left=city_loc, right=city_pop, on=\"city\", how=\"outer\")\n", "all_cities" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Of course `LEFT OUTER JOIN` is also available by setting `how=\"left\"`: only the cities present in the left `DataFrame` end up in the result. Similarly, with `how=\"right\"` only cities in the right `DataFrame` appear in the result. For example:" ] }, { "cell_type": "code", "execution_count": 138, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
state_xcitylatlngpopulationstate_y
0CASan Francisco37.781334-122.416728808976California
1NYNew York40.705649-74.0083448363710New-York
2FLMiami25.791100-80.320733413201Florida
3NaNHoustonNaNNaN2242193Texas
\n", "
" ], "text/plain": [ " state_x city lat lng population state_y\n", "0 CA San Francisco 37.781334 -122.416728 808976 California\n", "1 NY New York 40.705649 -74.008344 8363710 New-York\n", "2 FL Miami 25.791100 -80.320733 413201 Florida\n", "3 NaN Houston NaN NaN 2242193 Texas" ] }, "execution_count": 138, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.merge(left=city_loc, right=city_pop, on=\"city\", how=\"right\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If the key to join on is actually in one (or both) `DataFrame`'s index, you must use `left_index=True` and/or `right_index=True`. If the key column names differ, you must use `left_on` and `right_on`. For example:" ] }, { "cell_type": "code", "execution_count": 139, "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", " \n", " \n", " \n", " \n", "
state_xcitylatlngpopulationnamestate_y
0CASan Francisco37.781334-122.416728808976San FranciscoCalifornia
1NYNew York40.705649-74.0083448363710New YorkNew-York
2FLMiami25.791100-80.320733413201MiamiFlorida
\n", "
" ], "text/plain": [ " state_x city lat lng population name \\\n", "0 CA San Francisco 37.781334 -122.416728 808976 San Francisco \n", "1 NY New York 40.705649 -74.008344 8363710 New York \n", "2 FL Miami 25.791100 -80.320733 413201 Miami \n", "\n", " state_y \n", "0 California \n", "1 New-York \n", "2 Florida " ] }, "execution_count": 139, "metadata": {}, "output_type": "execute_result" } ], "source": [ "city_pop2 = city_pop.copy()\n", "city_pop2.columns = [\"population\", \"name\", \"state\"]\n", "pd.merge(left=city_loc, right=city_pop2, left_on=\"city\", right_on=\"name\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Concatenation\n", "Rather than joining `DataFrame`s, we may just want to concatenate them. That's what `concat` is for:" ] }, { "cell_type": "code", "execution_count": 140, "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", " \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", " \n", " \n", "
citylatlngpopulationstate
0San Francisco37.781334-122.416728NaNCA
1New York40.705649-74.008344NaNNY
2Miami25.791100-80.320733NaNFL
3Cleveland41.473508-81.739791NaNOH
4Salt Lake City40.755851-111.896657NaNUT
3San FranciscoNaNNaN808976California
4New YorkNaNNaN8363710New-York
5MiamiNaNNaN413201Florida
6HoustonNaNNaN2242193Texas
\n", "
" ], "text/plain": [ " city lat lng population state\n", "0 San Francisco 37.781334 -122.416728 NaN CA\n", "1 New York 40.705649 -74.008344 NaN NY\n", "2 Miami 25.791100 -80.320733 NaN FL\n", "3 Cleveland 41.473508 -81.739791 NaN OH\n", "4 Salt Lake City 40.755851 -111.896657 NaN UT\n", "3 San Francisco NaN NaN 808976 California\n", "4 New York NaN NaN 8363710 New-York\n", "5 Miami NaN NaN 413201 Florida\n", "6 Houston NaN NaN 2242193 Texas" ] }, "execution_count": 140, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result_concat = pd.concat([city_loc, city_pop])\n", "result_concat" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that this operation aligned the data horizontally (by columns) but not vertically (by rows). In this example, we end up with multiple rows having the same index (eg. 3). Pandas handles this rather gracefully:" ] }, { "cell_type": "code", "execution_count": 141, "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", "
citylatlngpopulationstate
3Cleveland41.473508-81.739791NaNOH
3San FranciscoNaNNaN808976California
\n", "
" ], "text/plain": [ " city lat lng population state\n", "3 Cleveland 41.473508 -81.739791 NaN OH\n", "3 San Francisco NaN NaN 808976 California" ] }, "execution_count": 141, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result_concat.loc[3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or you can tell pandas to just ignore the index:" ] }, { "cell_type": "code", "execution_count": 142, "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", " \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", " \n", " \n", "
citylatlngpopulationstate
0San Francisco37.781334-122.416728NaNCA
1New York40.705649-74.008344NaNNY
2Miami25.791100-80.320733NaNFL
3Cleveland41.473508-81.739791NaNOH
4Salt Lake City40.755851-111.896657NaNUT
5San FranciscoNaNNaN808976California
6New YorkNaNNaN8363710New-York
7MiamiNaNNaN413201Florida
8HoustonNaNNaN2242193Texas
\n", "
" ], "text/plain": [ " city lat lng population state\n", "0 San Francisco 37.781334 -122.416728 NaN CA\n", "1 New York 40.705649 -74.008344 NaN NY\n", "2 Miami 25.791100 -80.320733 NaN FL\n", "3 Cleveland 41.473508 -81.739791 NaN OH\n", "4 Salt Lake City 40.755851 -111.896657 NaN UT\n", "5 San Francisco NaN NaN 808976 California\n", "6 New York NaN NaN 8363710 New-York\n", "7 Miami NaN NaN 413201 Florida\n", "8 Houston NaN NaN 2242193 Texas" ] }, "execution_count": 142, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.concat([city_loc, city_pop], ignore_index=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that when a column does not exist in a `DataFrame`, it acts as if it was filled with `NaN` values. If we set `join=\"inner\"`, then only columns that exist in *both* `DataFrame`s are returned:" ] }, { "cell_type": "code", "execution_count": 143, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
citystate
0San FranciscoCA
1New YorkNY
2MiamiFL
3ClevelandOH
4Salt Lake CityUT
3San FranciscoCalifornia
4New YorkNew-York
5MiamiFlorida
6HoustonTexas
\n", "
" ], "text/plain": [ " city state\n", "0 San Francisco CA\n", "1 New York NY\n", "2 Miami FL\n", "3 Cleveland OH\n", "4 Salt Lake City UT\n", "3 San Francisco California\n", "4 New York New-York\n", "5 Miami Florida\n", "6 Houston Texas" ] }, "execution_count": 143, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.concat([city_loc, city_pop], join=\"inner\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can concatenate `DataFrame`s horizontally instead of vertically by setting `axis=1`:" ] }, { "cell_type": "code", "execution_count": 144, "metadata": { "collapsed": false, "scrolled": true }, "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", " \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", " \n", " \n", "
statecitylatlngpopulationcitystate
0CASan Francisco37.781334-122.416728NaNNaNNaN
1NYNew York40.705649-74.008344NaNNaNNaN
2FLMiami25.791100-80.320733NaNNaNNaN
3OHCleveland41.473508-81.739791808976San FranciscoCalifornia
4UTSalt Lake City40.755851-111.8966578363710New YorkNew-York
5NaNNaNNaNNaN413201MiamiFlorida
6NaNNaNNaNNaN2242193HoustonTexas
\n", "
" ], "text/plain": [ " state city lat lng population city \\\n", "0 CA San Francisco 37.781334 -122.416728 NaN NaN \n", "1 NY New York 40.705649 -74.008344 NaN NaN \n", "2 FL Miami 25.791100 -80.320733 NaN NaN \n", "3 OH Cleveland 41.473508 -81.739791 808976 San Francisco \n", "4 UT Salt Lake City 40.755851 -111.896657 8363710 New York \n", "5 NaN NaN NaN NaN 413201 Miami \n", "6 NaN NaN NaN NaN 2242193 Houston \n", "\n", " state \n", "0 NaN \n", "1 NaN \n", "2 NaN \n", "3 California \n", "4 New-York \n", "5 Florida \n", "6 Texas " ] }, "execution_count": 144, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.concat([city_loc, city_pop], axis=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this case it really does not make much sense because the indices do not align well (eg. Cleveland and San Francisco end up on the same row, because they shared the index label `3`). So let's reindex the `DataFrame`s by city name before concatenating:" ] }, { "cell_type": "code", "execution_count": 145, "metadata": { "collapsed": false, "scrolled": true }, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
statelatlngpopulationstate
ClevelandOH41.473508-81.739791NaNNaN
HoustonNaNNaNNaN2242193Texas
MiamiFL25.791100-80.320733413201Florida
New YorkNY40.705649-74.0083448363710New-York
Salt Lake CityUT40.755851-111.896657NaNNaN
San FranciscoCA37.781334-122.416728808976California
\n", "
" ], "text/plain": [ " state lat lng population state\n", "Cleveland OH 41.473508 -81.739791 NaN NaN\n", "Houston NaN NaN NaN 2242193 Texas\n", "Miami FL 25.791100 -80.320733 413201 Florida\n", "New York NY 40.705649 -74.008344 8363710 New-York\n", "Salt Lake City UT 40.755851 -111.896657 NaN NaN\n", "San Francisco CA 37.781334 -122.416728 808976 California" ] }, "execution_count": 145, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.concat([city_loc.set_index(\"city\"), city_pop.set_index(\"city\")], axis=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This looks a lot like a `FULL OUTER JOIN`, except that the `state` columns were not renamed to `state_x` and `state_y`, and the `city` column is now the index." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `append` method is a useful shorthand for concatenating `DataFrame`s vertically:" ] }, { "cell_type": "code", "execution_count": 146, "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", " \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", " \n", " \n", "
citylatlngpopulationstate
0San Francisco37.781334-122.416728NaNCA
1New York40.705649-74.008344NaNNY
2Miami25.791100-80.320733NaNFL
3Cleveland41.473508-81.739791NaNOH
4Salt Lake City40.755851-111.896657NaNUT
3San FranciscoNaNNaN808976California
4New YorkNaNNaN8363710New-York
5MiamiNaNNaN413201Florida
6HoustonNaNNaN2242193Texas
\n", "
" ], "text/plain": [ " city lat lng population state\n", "0 San Francisco 37.781334 -122.416728 NaN CA\n", "1 New York 40.705649 -74.008344 NaN NY\n", "2 Miami 25.791100 -80.320733 NaN FL\n", "3 Cleveland 41.473508 -81.739791 NaN OH\n", "4 Salt Lake City 40.755851 -111.896657 NaN UT\n", "3 San Francisco NaN NaN 808976 California\n", "4 New York NaN NaN 8363710 New-York\n", "5 Miami NaN NaN 413201 Florida\n", "6 Houston NaN NaN 2242193 Texas" ] }, "execution_count": 146, "metadata": {}, "output_type": "execute_result" } ], "source": [ "city_loc.append(city_pop)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As always in pandas, the `append` method does *not* actually modify `city_loc`: it works on a copy and returns the modified copy." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Categories\n", "It is quite frequent to have values that represent categories, for example `1` for female and `2` for male, or `\"A\"` for Good, `\"B\"` for Average, `\"C\"` for Bad. These categorical values can be hard to read and cumbersome to handle, but fortunately pandas makes it easy. To illustrate this, let's take the `city_pop` `DataFrame` we created earlier, and add a column that represents a category:" ] }, { "cell_type": "code", "execution_count": 147, "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", "
populationcitystateeco_code
3808976San FranciscoCalifornia17
48363710New YorkNew-York17
5413201MiamiFlorida34
62242193HoustonTexas20
\n", "
" ], "text/plain": [ " population city state eco_code\n", "3 808976 San Francisco California 17\n", "4 8363710 New York New-York 17\n", "5 413201 Miami Florida 34\n", "6 2242193 Houston Texas 20" ] }, "execution_count": 147, "metadata": {}, "output_type": "execute_result" } ], "source": [ "city_eco = city_pop.copy()\n", "city_eco[\"eco_code\"] = [17, 17, 34, 20]\n", "city_eco" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Right now the `eco_code` column is full of apparently meaningless codes. Let's fix that. First, we will create a new categorical column based on the `eco_code`s:" ] }, { "cell_type": "code", "execution_count": 148, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "Int64Index([17, 20, 34], dtype='int64')" ] }, "execution_count": 148, "metadata": {}, "output_type": "execute_result" } ], "source": [ "city_eco[\"economy\"] = city_eco[\"eco_code\"].astype('category')\n", "city_eco[\"economy\"].cat.categories" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can give each category a meaningful name:" ] }, { "cell_type": "code", "execution_count": 149, "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", " \n", " \n", " \n", " \n", "
populationcitystateeco_codeeconomy
3808976San FranciscoCalifornia17Finance
48363710New YorkNew-York17Finance
5413201MiamiFlorida34Tourism
62242193HoustonTexas20Energy
\n", "
" ], "text/plain": [ " population city state eco_code economy\n", "3 808976 San Francisco California 17 Finance\n", "4 8363710 New York New-York 17 Finance\n", "5 413201 Miami Florida 34 Tourism\n", "6 2242193 Houston Texas 20 Energy" ] }, "execution_count": 149, "metadata": {}, "output_type": "execute_result" } ], "source": [ "city_eco[\"economy\"].cat.categories = [\"Finance\", \"Energy\", \"Tourism\"]\n", "city_eco" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that categorical values are sorted according to their categorical order, *not* their alphabetical order:" ] }, { "cell_type": "code", "execution_count": 150, "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", " \n", " \n", " \n", " \n", "
populationcitystateeco_codeeconomy
5413201MiamiFlorida34Tourism
62242193HoustonTexas20Energy
48363710New YorkNew-York17Finance
3808976San FranciscoCalifornia17Finance
\n", "
" ], "text/plain": [ " population city state eco_code economy\n", "5 413201 Miami Florida 34 Tourism\n", "6 2242193 Houston Texas 20 Energy\n", "4 8363710 New York New-York 17 Finance\n", "3 808976 San Francisco California 17 Finance" ] }, "execution_count": 150, "metadata": {}, "output_type": "execute_result" } ], "source": [ "city_eco.sort_values(by=\"economy\", ascending=False)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# What next?\n", "As you probably noticed by now, pandas is quite a large library with *many* features. Although we went through the most important features, there is still a lot to discover. Probably the best way to learn more is to get your hands dirty with some real-life data. It is also a good idea to go through pandas' excellent [documentation](http://pandas.pydata.org/pandas-docs/stable/index.html), in particular the [Cookbook](http://pandas.pydata.org/pandas-docs/stable/cookbook.html)." ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 0 }