Skip to content

Instantly share code, notes, and snippets.

@ebolyen
Created April 29, 2014 19:25
Show Gist options
  • Save ebolyen/a4890870a3129cf4c931 to your computer and use it in GitHub Desktop.
Save ebolyen/a4890870a3129cf4c931 to your computer and use it in GitHub Desktop.
Pandas Coordinates
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "Coordinates Example"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": "import pandas as pd\nimport numpy as np",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 113
},
{
"cell_type": "code",
"collapsed": false,
"input": "store = pd.HDFStore('store.h5', format='table')\nprint store",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "<class 'pandas.io.pytables.HDFStore'>\nFile path: store.h5\n/dfeq frame_table (typ->appendable,nrows->30,ncols->1,indexers->[index],dc->[number])\n"
}
],
"prompt_number": 114
},
{
"cell_type": "code",
"collapsed": false,
"input": "dfeq = pd.DataFrame({'number': np.arange(0,11)})\nif 'dfeq' in store:\n del store['dfeq']\nstore.append('dfeq', dfeq, data_columns=['number'])\nprint store",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "<class 'pandas.io.pytables.HDFStore'>\nFile path: store.h5\n/dfeq frame_table (typ->appendable,nrows->11,ncols->1,indexers->[index],dc->[number])\n"
}
],
"prompt_number": 115
},
{
"cell_type": "code",
"collapsed": false,
"input": "print type(store['dfeq'])\nprint store['dfeq']",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "<class 'pandas.core.frame.DataFrame'>\n number\n0 0\n1 1\n2 2\n3 3\n4 4\n5 5\n6 6\n7 7\n8 8\n9 9\n10 10\n\n[11 rows x 1 columns]\n"
}
],
"prompt_number": 116
},
{
"cell_type": "code",
"collapsed": false,
"input": "evens = [2,4,6,8,10]\nc_evens = store.select_as_coordinates('dfeq','number=evens')\nstore.select('dfeq', where=c_evens)",
"language": "python",
"metadata": {},
"outputs": [
{
"html": "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>number</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>2 </th>\n <td> 2</td>\n </tr>\n <tr>\n <th>4 </th>\n <td> 4</td>\n </tr>\n <tr>\n <th>6 </th>\n <td> 6</td>\n </tr>\n <tr>\n <th>8 </th>\n <td> 8</td>\n </tr>\n <tr>\n <th>10</th>\n <td> 10</td>\n </tr>\n </tbody>\n</table>\n<p>5 rows \u00d7 1 columns</p>\n</div>",
"output_type": "pyout",
"prompt_number": 117,
"text": " number\n2 2\n4 4\n6 6\n8 8\n10 10\n\n[5 rows x 1 columns]"
}
],
"prompt_number": 117
},
{
"cell_type": "code",
"collapsed": false,
"input": "odds = [1,3,5,7,9]\nprimes = [2,3,5,7]\n\nc_odds = store.select_as_coordinates('dfeq','number=odds')\nc_primes = store.select_as_coordinates('dfeq','number=primes')\n\nprint c_evens\nprint c_odds\nprint c_primes",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Int64Index([2, 4, 6, 8, 10], dtype='int64')\nInt64Index([1, 3, 5, 7, 9], dtype='int64')\nInt64Index([2, 3, 5, 7], dtype='int64')\n"
}
],
"prompt_number": 118
},
{
"cell_type": "code",
"collapsed": false,
"input": "c_evens - c_primes",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 119,
"text": "Int64Index([4, 6, 8, 10], dtype='int64')"
}
],
"prompt_number": 119
},
{
"cell_type": "code",
"collapsed": false,
"input": "c_evens + c_odds\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 120,
"text": "Int64Index([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], dtype='int64')"
}
],
"prompt_number": 120
},
{
"cell_type": "code",
"collapsed": false,
"input": "def join(c1, c2):\n return (c1 + c2)\n\ndef inner_join(c1, c2):\n return (c1 + c2) - (c2 - c1) - (c1 - c2)",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 121
},
{
"cell_type": "code",
"collapsed": false,
"input": "print join(c_evens, c_primes)",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "Int64Index([2, 3, 4, 5, 6, 7, 8, 10], dtype='int64')\n"
}
],
"prompt_number": 122
},
{
"cell_type": "code",
"collapsed": false,
"input": "inner_join(c_evens, c_primes)",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 123,
"text": "Int64Index([2], dtype='int64')"
}
],
"prompt_number": 123
},
{
"cell_type": "code",
"collapsed": false,
"input": "",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 123
},
{
"cell_type": "code",
"collapsed": false,
"input": "",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 123
},
{
"cell_type": "code",
"collapsed": false,
"input": "",
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment