Skip to content

Commit f831bb7

Browse files
committed
added matplotlib snippets
1 parent 4b864af commit f831bb7

12 files changed

+2576
-0
lines changed

.ipynb_checkpoints/matplotlib_back_to_back_bar_plot-checkpoint.ipynb

+208
Large diffs are not rendered by default.

.ipynb_checkpoints/matplotlib_bar_plot-checkpoint.ipynb

+209
Large diffs are not rendered by default.

.ipynb_checkpoints/matplotlib_grouped_bar_plot-checkpoint.ipynb

+231
Large diffs are not rendered by default.

.ipynb_checkpoints/matplotlib_stacked_bar_plot-checkpoint.ipynb

+235
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
{
2+
"metadata": {
3+
"name": "",
4+
"signature": "sha256:0b3578405548c7616429ca47caaa2b3a306e76ae8b61a31f1f791895aaba3885"
5+
},
6+
"nbformat": 3,
7+
"nbformat_minor": 0,
8+
"worksheets": [
9+
{
10+
"cells": [
11+
{
12+
"cell_type": "markdown",
13+
"metadata": {},
14+
"source": [
15+
"# Iterating Through The Rows Of Multiple Columns In Pandas\n",
16+
"\n",
17+
"This snippit was written by [Chris R. Albon](http://www.chrisralbon.com/) and is part of his collection of [well-documented Python snippits](https://github.com/chrisalbon/code_py). All code is written in Python 3 in iPython notebook and offered under the [Creative Commons Attribution-ShareAlike 4.0 International License](http://creativecommons.org/licenses/by-sa/4.0/\"><img alt=\"Creative Commons License)."
18+
]
19+
},
20+
{
21+
"cell_type": "code",
22+
"collapsed": false,
23+
"input": [
24+
"# Import modules\n",
25+
"import pandas as pd\n",
26+
"import numpy as np"
27+
],
28+
"language": "python",
29+
"metadata": {},
30+
"outputs": [],
31+
"prompt_number": 8
32+
},
33+
{
34+
"cell_type": "code",
35+
"collapsed": false,
36+
"input": [
37+
"raw_data = {'first_name': ['Jason', 'Jason', 'Tina', 'Jake', 'Amy'], \n",
38+
" 'last_name': ['Miller', 'Miller', 'Ali', 'Milner', 'Cooze'], \n",
39+
" 'age': [42, 42, 36, 24, 73], \n",
40+
" 'preTestScore': [4, 4, 31, 2, 3],\n",
41+
" 'postTestScore': [25, 25, 57, 62, 70]}\n",
42+
"df = pd.DataFrame(raw_data, columns = ['first_name', 'last_name', 'age', 'preTestScore', 'postTestScore'])\n",
43+
"df"
44+
],
45+
"language": "python",
46+
"metadata": {},
47+
"outputs": [
48+
{
49+
"html": [
50+
"<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
51+
"<table border=\"1\" class=\"dataframe\">\n",
52+
" <thead>\n",
53+
" <tr style=\"text-align: right;\">\n",
54+
" <th></th>\n",
55+
" <th>first_name</th>\n",
56+
" <th>last_name</th>\n",
57+
" <th>age</th>\n",
58+
" <th>preTestScore</th>\n",
59+
" <th>postTestScore</th>\n",
60+
" </tr>\n",
61+
" </thead>\n",
62+
" <tbody>\n",
63+
" <tr>\n",
64+
" <th>0</th>\n",
65+
" <td> Jason</td>\n",
66+
" <td> Miller</td>\n",
67+
" <td> 42</td>\n",
68+
" <td> 4</td>\n",
69+
" <td> 25</td>\n",
70+
" </tr>\n",
71+
" <tr>\n",
72+
" <th>1</th>\n",
73+
" <td> Jason</td>\n",
74+
" <td> Miller</td>\n",
75+
" <td> 42</td>\n",
76+
" <td> 4</td>\n",
77+
" <td> 25</td>\n",
78+
" </tr>\n",
79+
" <tr>\n",
80+
" <th>2</th>\n",
81+
" <td> Tina</td>\n",
82+
" <td> Ali</td>\n",
83+
" <td> 36</td>\n",
84+
" <td> 31</td>\n",
85+
" <td> 57</td>\n",
86+
" </tr>\n",
87+
" <tr>\n",
88+
" <th>3</th>\n",
89+
" <td> Jake</td>\n",
90+
" <td> Milner</td>\n",
91+
" <td> 24</td>\n",
92+
" <td> 2</td>\n",
93+
" <td> 62</td>\n",
94+
" </tr>\n",
95+
" <tr>\n",
96+
" <th>4</th>\n",
97+
" <td> Amy</td>\n",
98+
" <td> Cooze</td>\n",
99+
" <td> 73</td>\n",
100+
" <td> 3</td>\n",
101+
" <td> 70</td>\n",
102+
" </tr>\n",
103+
" </tbody>\n",
104+
"</table>\n",
105+
"</div>"
106+
],
107+
"metadata": {},
108+
"output_type": "pyout",
109+
"prompt_number": 9,
110+
"text": [
111+
" first_name last_name age preTestScore postTestScore\n",
112+
"0 Jason Miller 42 4 25\n",
113+
"1 Jason Miller 42 4 25\n",
114+
"2 Tina Ali 36 31 57\n",
115+
"3 Jake Milner 24 2 62\n",
116+
"4 Amy Cooze 73 3 70"
117+
]
118+
}
119+
],
120+
"prompt_number": 9
121+
},
122+
{
123+
"cell_type": "markdown",
124+
"metadata": {},
125+
"source": [
126+
"## Iterate Over The Rows Of Two Columns"
127+
]
128+
},
129+
{
130+
"cell_type": "code",
131+
"collapsed": false,
132+
"input": [
133+
"# Create an empty column for the full names\n",
134+
"df['full_name'] = np.NaN\n",
135+
"\n",
136+
"# Create an iteration counter\n",
137+
"i = 0\n",
138+
"\n",
139+
"# For each element in first_name and last_name,\n",
140+
"for first, last in zip(df['first_name'], df['last_name']):\n",
141+
" # Change the value of the i'th row in full_name \n",
142+
" # to the combination of the first and last name\n",
143+
" df['full_name'][i] = first + ' ' + last\n",
144+
" \n",
145+
" # Add one to the iteration counter\n",
146+
" i = i+1"
147+
],
148+
"language": "python",
149+
"metadata": {},
150+
"outputs": [],
151+
"prompt_number": 27
152+
},
153+
{
154+
"cell_type": "code",
155+
"collapsed": false,
156+
"input": [
157+
"# View the dataframe\n",
158+
"df"
159+
],
160+
"language": "python",
161+
"metadata": {},
162+
"outputs": [
163+
{
164+
"html": [
165+
"<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
166+
"<table border=\"1\" class=\"dataframe\">\n",
167+
" <thead>\n",
168+
" <tr style=\"text-align: right;\">\n",
169+
" <th></th>\n",
170+
" <th>first_name</th>\n",
171+
" <th>last_name</th>\n",
172+
" <th>age</th>\n",
173+
" <th>preTestScore</th>\n",
174+
" <th>postTestScore</th>\n",
175+
" <th>full_name</th>\n",
176+
" </tr>\n",
177+
" </thead>\n",
178+
" <tbody>\n",
179+
" <tr>\n",
180+
" <th>0</th>\n",
181+
" <td> Jason</td>\n",
182+
" <td> Miller</td>\n",
183+
" <td> 42</td>\n",
184+
" <td> 4</td>\n",
185+
" <td> 25</td>\n",
186+
" <td> Jason Miller</td>\n",
187+
" </tr>\n",
188+
" <tr>\n",
189+
" <th>1</th>\n",
190+
" <td> Jason</td>\n",
191+
" <td> Miller</td>\n",
192+
" <td> 42</td>\n",
193+
" <td> 4</td>\n",
194+
" <td> 25</td>\n",
195+
" <td> Jason Miller</td>\n",
196+
" </tr>\n",
197+
" <tr>\n",
198+
" <th>2</th>\n",
199+
" <td> Tina</td>\n",
200+
" <td> Ali</td>\n",
201+
" <td> 36</td>\n",
202+
" <td> 31</td>\n",
203+
" <td> 57</td>\n",
204+
" <td> Tina Ali</td>\n",
205+
" </tr>\n",
206+
" <tr>\n",
207+
" <th>3</th>\n",
208+
" <td> Jake</td>\n",
209+
" <td> Milner</td>\n",
210+
" <td> 24</td>\n",
211+
" <td> 2</td>\n",
212+
" <td> 62</td>\n",
213+
" <td> Jake Milner</td>\n",
214+
" </tr>\n",
215+
" <tr>\n",
216+
" <th>4</th>\n",
217+
" <td> Amy</td>\n",
218+
" <td> Cooze</td>\n",
219+
" <td> 73</td>\n",
220+
" <td> 3</td>\n",
221+
" <td> 70</td>\n",
222+
" <td> Amy Cooze</td>\n",
223+
" </tr>\n",
224+
" </tbody>\n",
225+
"</table>\n",
226+
"</div>"
227+
],
228+
"metadata": {},
229+
"output_type": "pyout",
230+
"prompt_number": 28,
231+
"text": [
232+
" first_name last_name age preTestScore postTestScore full_name\n",
233+
"0 Jason Miller 42 4 25 Jason Miller\n",
234+
"1 Jason Miller 42 4 25 Jason Miller\n",
235+
"2 Tina Ali 36 31 57 Tina Ali\n",
236+
"3 Jake Milner 24 2 62 Jake Milner\n",
237+
"4 Amy Cooze 73 3 70 Amy Cooze"
238+
]
239+
}
240+
],
241+
"prompt_number": 28
242+
}
243+
],
244+
"metadata": {}
245+
}
246+
]
247+
}

0 commit comments

Comments
 (0)