{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "> This is one of the 100 recipes of the [IPython Cookbook](http://ipython-books.github.io/), the definitive guide to high-performance scientific computing and data science in Python.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 4.11. Manipulating large heterogeneous tables with HDF5 and PyTables" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n", "import tables as tb" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We create a new HDF5 file." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "f = tb.open_file('myfile.h5', 'w')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will create a HDF5 table with two columns: the name of a city (a string with 64 characters at most), and its population (a 32 bit integer)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "dtype = np.dtype([('city', 'S64'), ('population', 'i4')])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, we create the table in '/table1'." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "table = f.create_table('/', 'table1', dtype)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's add a few rows." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "table.append([('Brussels', 1138854),\n", " ('London', 8308369),\n", " ('Paris', 2243833)])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "After adding rows, we need to flush the table to commit the changes on disk." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "table.flush()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Data can be obtained from the table with a lot of different ways in PyTables. The easiest but less efficient way is to load the entire table in memory, which returns a NumPy array." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "table[:]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is also possible to load a particular column (and all rows)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "table.col('city')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When dealing with a large number of rows, we can make a SQL-like query in the table to load all rows that satisfy particular conditions." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "[row['city'] for row in table.where('population>2e6')]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, we can access particular rows knowing their indices." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "table[1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Clean-up." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "f.close()\n", "import os\n", "os.remove('myfile.h5')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).\n", "\n", "> [IPython Cookbook](http://ipython-books.github.io/), by [Cyrille Rossant](http://cyrille.rossant.net), Packt Publishing, 2014 (500 pages)." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.4.2" } }, "nbformat": 4, "nbformat_minor": 0 }