Created
September 4, 2016 12:05
-
-
Save butsugiri/d6cc987a479b3f4b88cd8c142d451a74 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"%matplotlib inline\n", | |
"import pandas as pd\n", | |
"import matplotlib.pyplot as plt\n", | |
"import numpy.random as rnd\n", | |
"import numpy as np\n", | |
"import matplotlib\n", | |
"import seaborn as sns" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"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>Date</th>\n", | |
" <th>Name</th>\n", | |
" <th>Company</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>2003-01-02 00:00:00</td>\n", | |
" <td>Shun</td>\n", | |
" <td>Anger</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>2003-01-03 00:00:00</td>\n", | |
" <td>Shun</td>\n", | |
" <td>Quit</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>2003-01-03 00:00:00</td>\n", | |
" <td>Shun</td>\n", | |
" <td>Happy</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>2003-01-05 00:00:00</td>\n", | |
" <td>Shun</td>\n", | |
" <td>Happy</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>2003-01-05 00:00:00</td>\n", | |
" <td>Shun</td>\n", | |
" <td>Happy</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>5</th>\n", | |
" <td>2003-01-02 00:00:00</td>\n", | |
" <td>John</td>\n", | |
" <td>Quit</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>6</th>\n", | |
" <td>2003-01-05 00:00:00</td>\n", | |
" <td>John</td>\n", | |
" <td>Happy</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>7</th>\n", | |
" <td>2003-01-06 00:00:00</td>\n", | |
" <td>John</td>\n", | |
" <td>Sad</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>8</th>\n", | |
" <td>2003-01-07 00:00:00</td>\n", | |
" <td>John</td>\n", | |
" <td>Anger</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" Date Name Company\n", | |
"0 2003-01-02 00:00:00 Shun Anger\n", | |
"1 2003-01-03 00:00:00 Shun Quit\n", | |
"2 2003-01-03 00:00:00 Shun Happy\n", | |
"3 2003-01-05 00:00:00 Shun Happy\n", | |
"4 2003-01-05 00:00:00 Shun Happy\n", | |
"5 2003-01-02 00:00:00 John Quit\n", | |
"6 2003-01-05 00:00:00 John Happy\n", | |
"7 2003-01-06 00:00:00 John Sad\n", | |
"8 2003-01-07 00:00:00 John Anger" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df = pd.read_csv(\"./ch09/toy.csv\")\n", | |
"df" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 24, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"actions = [\"Anger\", \"Quit\", \"Happy\", \"Sad\"]\n", | |
"def func(df):\n", | |
" ap = df[\"Company\"][df[\"Company\"] == \"Anger\"].count()\n", | |
" qp = df[\"Company\"][df[\"Company\"] == \"Quit\"].count()\n", | |
" hp = df[\"Company\"][df[\"Company\"] == \"Happy\"].count()\n", | |
" return ap + qp + hp\n", | |
" \n", | |
" pass" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"df = df.groupby([\"Name\", \"Date\"]).apply(func).reset_index()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 42, | |
"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>Name</th>\n", | |
" <th>Date</th>\n", | |
" <th>0</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>John</td>\n", | |
" <td>2003-01-02 00:00:00</td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>John</td>\n", | |
" <td>2003-01-05 00:00:00</td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>John</td>\n", | |
" <td>2003-01-06 00:00:00</td>\n", | |
" <td>0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>John</td>\n", | |
" <td>2003-01-07 00:00:00</td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>Shun</td>\n", | |
" <td>2003-01-02 00:00:00</td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>5</th>\n", | |
" <td>Shun</td>\n", | |
" <td>2003-01-03 00:00:00</td>\n", | |
" <td>2</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>6</th>\n", | |
" <td>Shun</td>\n", | |
" <td>2003-01-05 00:00:00</td>\n", | |
" <td>2</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" Name Date 0\n", | |
"0 John 2003-01-02 00:00:00 1\n", | |
"1 John 2003-01-05 00:00:00 1\n", | |
"2 John 2003-01-06 00:00:00 0\n", | |
"3 John 2003-01-07 00:00:00 1\n", | |
"4 Shun 2003-01-02 00:00:00 1\n", | |
"5 Shun 2003-01-03 00:00:00 2\n", | |
"6 Shun 2003-01-05 00:00:00 2" | |
] | |
}, | |
"execution_count": 42, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 43, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"df.columns = [\"Name\", \"Date\", \"Action\"]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 48, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Object `fillna` not found.\n" | |
] | |
} | |
], | |
"source": [ | |
"df.pivot(index=\"Date\", columns=\"Name\", values=\"Action\").fillna?" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 55, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th>Name</th>\n", | |
" <th>John</th>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>Date</th>\n", | |
" <th></th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>2003-01-02 00:00:00</th>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2003-01-03 00:00:00</th>\n", | |
" <td>0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2003-01-05 00:00:00</th>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2003-01-06 00:00:00</th>\n", | |
" <td>0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2003-01-07 00:00:00</th>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
"Name John\n", | |
"Date \n", | |
"2003-01-02 00:00:00 1\n", | |
"2003-01-03 00:00:00 0\n", | |
"2003-01-05 00:00:00 1\n", | |
"2003-01-06 00:00:00 0\n", | |
"2003-01-07 00:00:00 1" | |
] | |
}, | |
"execution_count": 55, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df.pivot(index=\"Date\", columns=\"Name\", values=\"Action\").fillna(0).ix[:,0:1]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"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