Skip to content

Instantly share code, notes, and snippets.

@taldcroft
Created July 24, 2014 16:44
Show Gist options
  • Select an option

  • Save taldcroft/cccf5fac271f2ac49e0d to your computer and use it in GitHub Desktop.

Select an option

Save taldcroft/cccf5fac271f2ac49e0d to your computer and use it in GitHub Desktop.
{
"metadata": {
"name": "",
"signature": "sha256:4a49c6d0edca23508b557df3528e49f4d68874bce29b47251e2bfe77b10e39f2"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"from astropy.table import Table\n",
"import numpy as np"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Experiment with a table having 1000 rows and 100 columns"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"t = Table(np.arange(100000).reshape(1000, 100))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Iterate with current Rows and access one of the table columns in each row"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def access1(t):\n",
" for row in t:\n",
" col25 = row['col25'] + 1"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"timeit access1(t)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"100 loops, best of 3: 8.42 ms per loop\n"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Iterate with something like proposed Row and access one table column in each row"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def access2(t):\n",
" cols = t.columns.values()\n",
" for i in xrange(len(t)):\n",
" rowvals = [col[i] for col in cols]\n",
" col25 = rowvals[25] + 1"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"timeit access2(t)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"10 loops, best of 3: 27.3 ms per loop\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Iterate with current Row but do someting with every table column"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def access3(t):\n",
" colnames = t.colnames\n",
" for row in t:\n",
" for icol in xrange(len(colnames)):\n",
" col = row[icol] + 1"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 15
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"timeit access3(t)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1 loops, best of 3: 373 ms per loop\n"
]
}
],
"prompt_number": 16
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Iterate with proposed Row and do something with every table column"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def access4(t):\n",
" cols = t.columns.values()\n",
" for i in xrange(len(t)):\n",
" for col in cols:\n",
" colval = col[i] + 1"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 17
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"timeit access4(t)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"10 loops, best of 3: 130 ms per loop\n"
]
}
],
"prompt_number": 18
},
{
"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