diff --git a/index.ipynb b/index.ipynb index 73a0b15..aa9d9b8 100644 --- a/index.ipynb +++ b/index.ipynb @@ -11,6 +11,7 @@ "## Tools\n", "* [NumPy](tools_numpy.ipynb)\n", "* [Matplotlib](tools_matplotlib.ipynb)\n", + "* [Pandas](tools_pandas.ipynb)\n", "\n", "**This work is in progress, more notebooks are coming soon...**" ] diff --git a/tools_pandas.ipynb b/tools_pandas.ipynb index fb73ed7..f7007d5 100644 --- a/tools_pandas.ipynb +++ b/tools_pandas.ipynb @@ -5,9 +5,6908 @@ "metadata": {}, "source": [ "# Tools - pandas\n", - "*The `pandas` library provides high-performance, easy-to-use data structures and data analysis tools.*\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 a spreadsheet (including column names and row labels).*\n", "\n", - "Coming soon..." + "**Prerequisites:**\n", + "* NumPy – if you are not familiar with NumPy, we recommend that you go through the [NumPy tutorial](tools_numpy.ipynb) now.\n", + "\n", + "## 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 similar to a column in a spreadsheet (with a column name and row labels).\n", + "* `DataFrame` objects. You can see this as a full spreadsheet (with column names and row labels).\n", + "* `Panel` objects. You can see a `Panel` a a dictionary of `DataFrame`s (less used). These are less used, so we will not discuss them here.\n", + "\n", + "### 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 + pd.Series([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`:" + ] + }, + { + "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 index of the item in the `Series` 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 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": [ + "Slicing a `Series` also slices the index labels:" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "bob 83\n", + "charles 112\n", + "dtype: int64" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s2[1:3]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This can lead to unexpected results when using the default labels, so be careful:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "0 1000\n", + "1 1001\n", + "2 1002\n", + "3 1003\n", + "dtype: int64" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "surprise = pd.Series([1000, 1001, 1002, 1003])\n", + "surprise" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "2 1002\n", + "3 1003\n", + "dtype: int64" + ] + }, + "execution_count": 13, + "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": 14, + "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 you can access elements by location using the `iloc` attribute:" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "1002" + ] + }, + "execution_count": 15, + "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": 16, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "alice 68\n", + "bob 83\n", + "colin 86\n", + "darwin 68\n", + "dtype: int64" + ] + }, + "execution_count": 16, + "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 passing a second argument to the constructor with the list of desired index labels:" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "colin 86\n", + "alice 68\n", + "dtype: int64" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s4 = pd.Series(weights, [\"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": 18, + "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": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "print(s2.keys())\n", + "print(s3.keys())\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": 19, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "s2 = [ 68 83 112 68]\n", + "s5 = [1000 1000 1000 1000]\n", + "s2 + s5 =\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": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s5 = pd.Series([1000,1000,1000,1000])\n", + "print(\"s2 =\", s2.values)\n", + "print(\"s5 =\", s5.values)\n", + "print(\"s2 + s5 =\")\n", + "s2 + s5" + ] + }, + { + "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": 20, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "life 42\n", + "universe 42\n", + "everything 42\n", + "dtype: int64" + ] + }, + "execution_count": 20, + "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": 21, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "bob 83\n", + "alice 68\n", + "Name: weights, dtype: int64" + ] + }, + "execution_count": 21, + "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": 85, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAW0AAAEACAYAAAB4ayemAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAH+BJREFUeJzt3XmUVNW5/vHvS8TgSBAHRBRBxUQjEZaKUdQSrgPOGgfi\nBERJ/DlHYxxzwZssTBxpjTEakQiRJEq8gVxNAIciKhqHYEOUC46gaDAIEbmI0PT7+2NXY9N0d42n\nTtWp57MWy+rqU+fsEvrtXfvsZ29zd0REpDp0iLsBIiKSOxVtEZEqoqItIlJFVLRFRKqIiraISBVR\n0RYRqSJZi7aZ9TCzp8zsNTOba2aXZp7vYmbTzWy+mU0zs87RN1dEpLZZtnnaZtYN6Obur5rZlsAr\nwInACOBjd7/ZzK4Gurj7NZG3WESkhmXtabv7P9391czjlcA8oAehcD+YOexB4KSoGikiIkHWnvYG\nB5vtCqSBrwPvuXuXZt9b5u7blLh9IiLSTM43IjNDI5OByzI97pbVXnl4EZGIbZLLQWa2CaFgT3T3\nKZmnl5jZDu6+JDPu/VEbr1UxFxEpgLtby+dy7Wk/ALzu7nXNnpsKDM88HgZMafmiZheuqT+jRo2K\nvQ16z3rPer/V/Z7bkrWnbWYHA2cBc81sNmEY5DrgZ8DDZvYdYCFwerZziYhIcbIWbXd/DvhSG9/+\nj9I2R0RE2qNEZARSqVTcTSg7vefkq7X3C5X5nvOa8lfQBcw86muIiCSNmeFF3IgUEZEKoKItIlJF\nVLRFRKqIiraISBVR0RYRqSIq2iIiVURFW0Skiqhoi4i0Yvp0GDIEKi1monCNiEgLS5ZAv36wejU8\n+WR4XG5thWtUtEVEmmlshGOOgf33D73sVavg9tvL3w4VbR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7k/E26qy2d7dl0DomAHbx9wezR4pNTPb\nEpgMXJbpcSdWPgGrBNkE6A/c7e79gVWEj9CJZWa9ge8DPYHuwJZmdma8rYpN7J2TqIr2YmCXZl/3\nyDyXaJmPjpOBie4+Je72lMHBwAlm9jbwW+BwM5sQc5ui9j7wnru/nPl6MqGIJ9l+wHPuvszd1wGP\nAgfF3KZyWWJmOwCYWTfgo5jbE1nRfgnY3cx6Zu4yDwVqYWbBA8Dr7l4Xd0PKwd2vc/dd3L034e/4\nqZaJ2KTJfFR+z8z6ZJ4aTPJvws4HDjSzTmZmhPec1JuvLT8xTgWGZx4PA2LvjEWyc427rzOzi4Hp\nhF8M49w9qX/JAJjZwcBZwFwzm034GHWdu/8l3pZJBC4FHjKzjsDbwIiY2xMpd6/PfIJ6BVgHzAbu\ni7dVpWdmk4AU0NXMFgGjgJ8Cj5jZd4CFwOnxtTBQuEZEpIroRqSISBVR0RYRqSIq2iIiVURFW0Sk\niqhoi4hUERVtEZEqoqItIlJFVLRFRKrI/weTvVXLditpUwAAAABJRU5ErkJggg==\n", + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "%matplotlib inline\n", + "import matplotlib.pyplot as plt\n", + "s7 = pd.Series([4,9,10,8,14,12,11,9,17,16,19,13], 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": [ + "## `DataFrame` objects\n", + "A DataFrame object represents a spreadsheet, with cell values, column names and row index labels. You can think of them as dictionaries of `Series` objects.\n", + "\n", + "### Creating a `DataFrame`\n", + "You can create a DataFrame by passing a dictionary of `Series` objects:" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
birthyearchildrenhobbyweight
alice1985NaNBiking68
bob19843Dancing83
charles19920Reading112
\n", + "
" + ], + "text/plain": [ + " birthyear children hobby weight\n", + "alice 1985 NaN Biking 68\n", + "bob 1984 3 Dancing 83\n", + "charles 1992 0 Reading 112" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "people_ids = [\"alice\", \"bob\", \"charles\"]\n", + "people_dict = {\n", + " \"weight\": pd.Series([68, 83, 112], index=people_ids),\n", + " \"birthyear\": pd.Series([1985, 1984, 1992], index=people_ids, name=\"year\"),\n", + " \"children\": pd.Series([np.nan, 3, 0], index=people_ids),\n", + " \"hobby\": pd.Series([\"Biking\", \"Dancing\", \"Reading\"], index=people_ids),\n", + "}\n", + "people = pd.DataFrame(people_dict)\n", + "people" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Note that DataFrames are displayed nicely in Jupyter notebooks! Also, note that `Series` names are ignored (`\"year\"` was dropped)." + ] + }, + { + "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": 24, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "alice 1985\n", + "bob 1984\n", + "charles 1992\n", + "Name: birthyear, dtype: int64" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "people[\"birthyear\"]" + ] + }, + { + "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": 25, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
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": 25, + "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`, and specify the column names and row index labels separately:" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "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
charles19920Reading112
\n", + "
" + ], + "text/plain": [ + " birthyear children hobby weight\n", + "alice 1985 nan Biking 68\n", + "bob 1984 3 Dancing 83\n", + "charles 1992 0 Reading 112" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "values = np.array([\n", + " [1985, np.nan, \"Biking\", 68],\n", + " [1984, 3, \"Dancing\", 83],\n", + " [1992, 0, \"Reading\", 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": [ + "Instead of an `ndarray`, you can also pass a `DataFrame` object:" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "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": 27, + "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": 28, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
birthyearchildrenhobbyweight
alice1985NaNBiking68
bob19843Dancing83
charles19920Reading112
\n", + "
" + ], + "text/plain": [ + " birthyear children hobby weight\n", + "alice 1985 NaN Biking 68\n", + "bob 1984 3 Dancing 83\n", + "charles 1992 0 Reading 112" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "people = pd.DataFrame({\n", + " \"birthyear\": {\"alice\":1985, \"bob\": 1984, \"charles\": 1992},\n", + " \"hobby\": {\"alice\":\"Biking\", \"bob\": \"Dancing\", \"charles\": \"Reading\"},\n", + " \"weight\": {\"alice\":68, \"bob\": 83, \"charles\": 112},\n", + " \"children\": {\"alice\":np.nan, \"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": 29, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
privatepublic
childrenweightbirthyearhobby
Londoncharles01121992Reading
ParisaliceNaN681985Biking
bob3831984Dancing
\n", + "
" + ], + "text/plain": [ + " private public \n", + " children weight birthyear hobby\n", + "London charles 0 112 1992 Reading\n", + "Paris alice NaN 68 1985 Biking\n", + " bob 3 83 1984 Dancing" + ] + }, + "execution_count": 29, + "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\", (\"London\",\"charles\"): \"Reading\"},\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": 30, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
birthyearhobby
Londoncharles1992Reading
Parisalice1985Biking
bob1984Dancing
\n", + "
" + ], + "text/plain": [ + " birthyear hobby\n", + "London charles 1992 Reading\n", + "Paris alice 1985 Biking\n", + " bob 1984 Dancing" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d5[\"public\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "London charles Reading\n", + "Paris alice Biking\n", + " bob Dancing\n", + "Name: (public, hobby), dtype: object" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d5[\"public\", \"hobby\"] # Same result as d4[\"public\"][\"hobby\"]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Accessing rows\n", + "Let's go back to the `people` `DataFrame`:" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
birthyearchildrenhobbyweight
alice1985NaNBiking68
bob19843Dancing83
charles19920Reading112
\n", + "
" + ], + "text/plain": [ + " birthyear children hobby weight\n", + "alice 1985 NaN Biking 68\n", + "bob 1984 3 Dancing 83\n", + "charles 1992 0 Reading 112" + ] + }, + "execution_count": 32, + "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": 33, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "birthyear 1992\n", + "children 0\n", + "hobby Reading\n", + "weight 112\n", + "Name: charles, dtype: object" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "people.loc[\"charles\"]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You can also access rows by location using the `iloc` attribute:" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "birthyear 1992\n", + "children 0\n", + "hobby Reading\n", + "weight 112\n", + "Name: charles, dtype: object" + ] + }, + "execution_count": 34, + "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": 35, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
birthyearchildrenhobbyweight
bob19843Dancing83
charles19920Reading112
\n", + "
" + ], + "text/plain": [ + " birthyear children hobby weight\n", + "bob 1984 3 Dancing 83\n", + "charles 1992 0 Reading 112" + ] + }, + "execution_count": 35, + "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": 36, + "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
charles19920Reading112
\n", + "
" + ], + "text/plain": [ + " birthyear children hobby weight\n", + "alice 1985 NaN Biking 68\n", + "charles 1992 0 Reading 112" + ] + }, + "execution_count": 36, + "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": 37, + "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": 37, + "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": 38, + "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
charles19920Reading112
\n", + "
" + ], + "text/plain": [ + " birthyear children hobby weight\n", + "alice 1985 NaN Biking 68\n", + "bob 1984 3 Dancing 83\n", + "charles 1992 0 Reading 112" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "people" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "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
charlesReading11224False
\n", + "
" + ], + "text/plain": [ + " hobby weight age over 30\n", + "alice Biking 68 31 True\n", + "bob Dancing 83 32 True\n", + "charles Reading 112 24 False" + ] + }, + "execution_count": 39, + "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": 40, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "alice 1985\n", + "bob 1984\n", + "charles 1992\n", + "Name: birthyear, dtype: int64" + ] + }, + "execution_count": 40, + "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": 41, + "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
charlesReading11224False5
\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 Reading 112 24 False 5" + ] + }, + "execution_count": 41, + "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": 42, + "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
charlesReading18511224False5
\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 Reading 185 112 24 False 5" + ] + }, + "execution_count": 42, + "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": 43, + "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
charlesReading18511224False532.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 Reading 185 112 24 False 5 32.724617 True" + ] + }, + "execution_count": 43, + "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": 44, + "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": 45, + "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
charlesReading18511224False532.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 Reading 185 112 24 False 5 32.724617 \n", + "\n", + " overweight \n", + "alice False \n", + "bob True \n", + "charles True " + ] + }, + "execution_count": 45, + "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": 46, + "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": 47, + "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
charlesReading18511224False532.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 Reading 185 112 24 False 5 32.724617 \n", + "\n", + " overweight \n", + "alice False \n", + "bob True \n", + "charles True " + ] + }, + "execution_count": 47, + "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": 48, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "alice False\n", + "bob True\n", + "charles True\n", + "dtype: bool" + ] + }, + "execution_count": 48, + "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": 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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
hobbyheightweightageover 30petsbody_mass_index
aliceBiking1726831TrueNaN22.985398
bobDancing1818332True025.335002
charlesReading18511224False532.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 Reading 185 112 24 False 5 32.724617" + ] + }, + "execution_count": 49, + "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": 50, + "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
charlesReading18511224False532.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 Reading 185 112 24 False 5 32.724617 \n", + "\n", + " overweight \n", + "alice False \n", + "bob False \n", + "charles True " + ] + }, + "execution_count": 50, + "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": 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", + " \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": 51, + "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": 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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
hobbyheightweightageover 30petsbody_mass_indexoverweight
charlesReading18511224False532.724617True
bobDancing1818332True025.335002False
aliceBiking1726831TrueNaN22.985398False
\n", + "
" + ], + "text/plain": [ + " hobby height weight age over 30 pets body_mass_index \\\n", + "charles Reading 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": 52, + "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": 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", + " \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.724617185ReadingFalseTrue5112
\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 Reading False True 5 \n", + "\n", + " weight \n", + "alice 68 \n", + "bob 83 \n", + "charles 112 " + ] + }, + "execution_count": 53, + "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": 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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
agebody_mass_indexheighthobbyover 30overweightpetsweight
charles2432.724617185ReadingFalseTrue5112
alice3122.985398172BikingTrueFalseNaN68
bob3225.335002181DancingTrueFalse083
\n", + "
" + ], + "text/plain": [ + " age body_mass_index height hobby over 30 overweight pets \\\n", + "charles 24 32.724617 185 Reading 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": 54, + "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": 55, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAEQCAYAAACgBo8fAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3Xt0VPW99/H3l3sIhFsEhGAIGCEWtIpS7VGYar09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Rqf671S19btpDl95U7u5rw+WngfOBLf75\nxQyWA/MILu+1xd23hO1PxW1jGXB1uHx9eL8tDX3vG4BP/fMLhHwITA2X/8HM/kpwVsiTw9sB4Ej4\nq+HrwJFw3TeB5Wb2z3Suy/JVd6/14Gx8u4AJBKH8/9z9mLsfIjgjJWaWCXwZWGnBhUIeC9fH3T8j\n+IJYQxD+BzpRg0Sc+tClNzXvB99PsCecSMLTArv7J2a2y8y+ApxJ++ewbzglcn3ccsP9QRZcgeYu\nYI67HwzP+DfM3evMbC7Bl843gduA8939VjM7E7gU+KuZne7u+9qpIb4OgDra/r/XcKnB1i4AfgrB\nXn7zS7dJP6c9dOlNuWb2pXD5H4G/AFPNbFrYdjVQSNCVkGtmeWH7Fc22s5Rgr/05d+/MwdJEXxJZ\nQCVwKLwAxyUAZjacoCtmFfAdghDFzKa5+1/cfTHBRTqmdOL1m9fxR+ByMxtqZiOBrwGEe+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": 56, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYwAAAEPCAYAAABRHfM8AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAGFVJREFUeJzt3XuUnXV97/H3N+RGEsFwSwQSBI0YoIggxCUgW7lJPQUU\niohFQJF6KD0Vag6ksBZZq7bcDkdaKloQ07Q2QjhVwC4gAs2oIIISJNyEVARCgAgCgXAZIPmeP/aT\nOBlmwm/PzL5MeL/WmsWzn8ven+zJkw/PPTITSZLeyoh2B5AkDQ8WhiSpiIUhSSpiYUiSilgYkqQi\nFoYkqUhTCyMiLo+I5RGxuMe48yPigYj4VUT8R0Rs0mParIhYUk0/qJnZJEmNafYWxhzg4F7jfgTs\nnJm7AUuAWQARsRNwFDAdOAS4JCKiyfkkSYWaWhiZeQvwXK9xN2Xm6urlz4Ftq+FDgSsy843MfIR6\nmezVzHySpHLtPobxBeC6angbYGmPacuqcZKkDtC2woiIM4HXM/N77cogSSo3sh0fGhHHA38MfLzH\n6GXAlB6vt63G9bW8N8CSpAHIzAEfG27FFkZUP/UXEZ8AZgKHZmZ3j/muBY6OiNERsT3wXuCO/t40\nMzv+5+yzz257BnOaczjnHA4Zh1POwWrqFkZEzANqwOYR8RhwNvA3wGjgxuokqJ9n5smZeX9EzAfu\nB14HTs6h+BNKkoZEUwsjM4/pY/Sc9cx/DnBO8xJJkgaq3WdJbdBqtVq7IxQx59Ay59AZDhlh+OQc\nrBiOe30iwr1VktSgiCA7/KC3JGkDYGFIkopYGJKkIhaGJKmIhSFJKmJhSJKKWBiSpCIWhiSpiIUh\nSSpiYUiSilgYkqQiFoYkqYiFIUkqYmFIkopYGJKkIhaGJKmIhSFJKmJhSJKKWBiSpCIj2x1AkvQH\nq1ev5vbbb2fFihXMmDGDiRMntjvSWm5hSFKHu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u8aC3Opm3N5eGWESMz8yXquHTgcmZeWqbY0mD5jEMaeh9sroQbyT1x7Ee39Y00hBxC0OS\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": 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", + "
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": 57, + "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": 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", + " \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": 58, + "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": 59, + "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": 59, + "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": 60, + "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": 60, + "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": 61, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "sep 7.75\n", + "oct 8.75\n", + "nov 7.50\n", + "dtype: float64" + ] + }, + "execution_count": 61, + "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": 62, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "sep False\n", + "oct True\n", + "nov False\n", + "dtype: bool" + ] + }, + "execution_count": 62, + "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": 63, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "alice True\n", + "bob True\n", + "charles False\n", + "darwin True\n", + "dtype: bool" + ] + }, + "execution_count": 63, + "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": 64, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "alice False\n", + "bob True\n", + "charles False\n", + "darwin True\n", + "dtype: bool" + ] + }, + "execution_count": 64, + "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": 65, + "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": 65, + "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": 66, + "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": 66, + "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": 67, + "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": 67, + "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": 68, + "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": 68, + "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": 69, + "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": 69, + "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": 70, + "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": 70, + "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": 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", + " \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": 71, + "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": 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", + " \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": 72, + "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": 73, + "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": 73, + "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": 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", + " \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": 74, + "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": 75, + "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": 75, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grades + better_bonus_points" + ] + }, + { + "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).\n", + "\n", + "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": 76, + "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": 76, + "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": [ + "### 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:" + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "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
charles4115NaNReading
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 Reading\n", + "colin NaN NaN NaN NaN Dancing\n", + "darwin 9 10 11 NaN Biking" + ] + }, + "execution_count": 77, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "final_grades[\"hobby\"] = [\"Biking\", \"Dancing\", \"Reading\", \"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": 78, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 78, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grouped_grades = final_grades.groupby(\"hobby\")\n", + "grouped_grades" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now let's compute the average grade per hobby:" + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "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
hobby
Biking8.5910NaN
Dancing10.0910NaN
Reading4.0115NaN
\n", + "
" + ], + "text/plain": [ + " sep oct nov dec\n", + "hobby \n", + "Biking 8.5 9 10 NaN\n", + "Dancing 10.0 9 10 NaN\n", + "Reading 4.0 11 5 NaN" + ] + }, + "execution_count": 79, + "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." + ] + }, + { + "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": 80, + "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": 80, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "much_data = np.fromfunction(lambda x,y: (x+y*y)%17*11, (10000, 26))\n", + "large = pd.DataFrame(much_data, columns=list(\"ABCDEFGHIJKLMNOPQRSTUVWXYZ\"))\n", + "large[large%16==0] = np.nan\n", + "large.insert(3,\"some_text\", \"Blabla\")\n", + "large" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The `head` method returns the top 5 rows:" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "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": 81, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "large.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": 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", + " \n", + " \n", + " \n", + " \n", + " \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": 82, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "large.tail(n=2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The `info` method prints out a summary of each columns contents:" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "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.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": 84, + "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": 84, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "large.describe()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# To be continued...\n", + "Coming soon:\n", + "* categories\n", + "* pivot-tables\n", + "* stacking\n", + "* merging\n", + "* time series\n", + "* loading & saving" ] } ],