Skip to content

Commit

Permalink
Convert ch12 raw cells to Markdown with Python highlighting. Close we…
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm committed Sep 28, 2017
1 parent 31ac417 commit 4323f2a
Showing 1 changed file with 46 additions and 24 deletions.
70 changes: 46 additions & 24 deletions ch12.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -751,79 +751,91 @@
]
},
{
"cell_type": "raw",
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"```python\n",
"df = load_data()\n",
"df2 = df[df['col2'] < 0]\n",
"df2['col1_demeaned'] = df2['col1'] - df2['col1'].mean()\n",
"result = df2.groupby('key').col1_demeaned.std()"
"result = df2.groupby('key').col1_demeaned.std()\n",
"```"
]
},
{
"cell_type": "raw",
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"```python\n",
"# Usual non-functional way\n",
"df2 = df.copy()\n",
"df2['k'] = v\n",
"\n",
"# Functional assign way\n",
"df2 = df.assign(k=v)"
"df2 = df.assign(k=v)\n",
"```"
]
},
{
"cell_type": "raw",
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"```python\n",
"result = (df2.assign(col1_demeaned=df2.col1 - df2.col2.mean())\n",
" .groupby('key')\n",
" .col1_demeaned.std())"
" .col1_demeaned.std())\n",
"```"
]
},
{
"cell_type": "raw",
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"```python\n",
"df = load_data()\n",
"df2 = df[df['col2'] < 0]"
"df2 = df[df['col2'] < 0]\n",
"```"
]
},
{
"cell_type": "raw",
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"```python\n",
"df = (load_data()\n",
" [lambda x: x['col2'] < 0])"
" [lambda x: x['col2'] < 0])\n",
"```"
]
},
{
"cell_type": "raw",
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"```python\n",
"result = (load_data()\n",
" [lambda x: x.col2 < 0]\n",
" .assign(col1_demeaned=lambda x: x.col1 - x.col1.mean())\n",
" .groupby('key')\n",
" .col1_demeaned.std())"
" .col1_demeaned.std())\n",
"```"
]
},
{
Expand All @@ -837,71 +849,81 @@
]
},
{
"cell_type": "raw",
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"```python\n",
"a = f(df, arg1=v1)\n",
"b = g(a, v2, arg3=v3)\n",
"c = h(b, arg4=v4)"
"c = h(b, arg4=v4)\n",
"```"
]
},
{
"cell_type": "raw",
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"```python\n",
"result = (df.pipe(f, arg1=v1)\n",
" .pipe(g, v2, arg3=v3)\n",
" .pipe(h, arg4=v4))"
" .pipe(h, arg4=v4))\n",
"```"
]
},
{
"cell_type": "raw",
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"```python\n",
"g = df.groupby(['key1', 'key2'])\n",
"df['col1'] = df['col1'] - g.transform('mean')"
"df['col1'] = df['col1'] - g.transform('mean')\n",
"```"
]
},
{
"cell_type": "raw",
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"```python\n",
"def group_demean(df, by, cols):\n",
" result = df.copy()\n",
" g = df.groupby(by)\n",
" for c in cols:\n",
" result[c] = df[c] - g[c].transform('mean')\n",
" return result"
" return result\n",
"```"
]
},
{
"cell_type": "raw",
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"```python\n",
"result = (df[df.col1 < 0]\n",
" .pipe(group_demean, ['key1', 'key2'], ['col1']))"
" .pipe(group_demean, ['key1', 'key2'], ['col1']))\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"collapsed": true,
"deletable": true,
"editable": true
},
Expand Down Expand Up @@ -937,7 +959,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.0"
"version": "3.5.1"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 4323f2a

Please sign in to comment.