Skip to content

Instantly share code, notes, and snippets.

@davclark
Created August 1, 2016 02:01
Show Gist options
  • Save davclark/228bc2a73afbd6a13b12bed0682a736a to your computer and use it in GitHub Desktop.
Save davclark/228bc2a73afbd6a13b12bed0682a736a to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"class SelfConscious:\n",
" called = 0\n",
" \n",
" def __init__(self, label):\n",
" self.label = label\n",
" \n",
" def increment(self):\n",
" self.called += 1\n",
" print('object {} called {} times'.format(self.label, self.called))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"object 0 called 1 times\n",
"object 0 called 2 times\n"
]
}
],
"source": [
"sc = SelfConscious(0)\n",
"sc.increment()\n",
"sc.increment()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"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>sc</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>&lt;__main__.SelfConscious object at 0x10aa6b898&gt;</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>&lt;__main__.SelfConscious object at 0x10aa6b8d0&gt;</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>&lt;__main__.SelfConscious object at 0x10aa6b940&gt;</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>&lt;__main__.SelfConscious object at 0x10aa6ba20&gt;</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" sc\n",
"0 <__main__.SelfConscious object at 0x10aa6b898>\n",
"1 <__main__.SelfConscious object at 0x10aa6b8d0>\n",
"2 <__main__.SelfConscious object at 0x10aa6b940>\n",
"3 <__main__.SelfConscious object at 0x10aa6ba20>"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"scs = pd.DataFrame({'sc': [SelfConscious(label) for label in range(4)]})\n",
"scs"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"object 0 called 1 times\n",
"object 1 called 1 times\n",
"object 2 called 1 times\n",
"object 3 called 1 times\n"
]
},
{
"data": {
"text/plain": [
"0 None\n",
"1 None\n",
"2 None\n",
"3 None\n",
"Name: sc, dtype: object"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"scs.sc.apply(lambda x: x.increment())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment