Skip to content

Instantly share code, notes, and snippets.

@neuromusic
Created October 25, 2016 19:21
Show Gist options
  • Save neuromusic/421661ad8228aabaa3648ae44fcd74e2 to your computer and use it in GitHub Desktop.
Save neuromusic/421661ad8228aabaa3648ae44fcd74e2 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I recently came across a post about using generators in python.\n",
"\n",
"http://nvie.com/posts/use-more-iterators/\n",
"\n",
"The example pattern is very similar to a pattern I use when computing data that needs to be dumped into a dataframe.\n",
"\n",
"This notebook is a test (and confirmation) that pandas' DataFrame(data) will take a generator that yields dictionaries."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import random\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>a</th>\n",
" <th>ii</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>4</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>8</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>9</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>9</td>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>3</td>\n",
" <td>4</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" a ii\n",
"0 4 0\n",
"1 8 1\n",
"2 9 2\n",
"3 9 3\n",
"4 3 4"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def make_data():\n",
" result = []\n",
" for ii in range(10):\n",
" result.append(dict(\n",
" ii=ii,\n",
" a=random.randint(0,10),\n",
" ))\n",
" return result\n",
"\n",
"data = pd.DataFrame(make_data())\n",
"data.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now the generator version."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>a</th>\n",
" <th>ii</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>5</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>7</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>6</td>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>9</td>\n",
" <td>4</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" a ii\n",
"0 5 0\n",
"1 0 1\n",
"2 7 2\n",
"3 6 3\n",
"4 9 4"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def make_data():\n",
" for ii in range(10):\n",
" yield dict(\n",
" ii=ii,\n",
" a=random.randint(0,10),\n",
" )\n",
"\n",
"data = pd.DataFrame(make_data())\n",
"data.head()"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [dro]",
"language": "python",
"name": "Python [dro]"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment