/
Day_14_PfDA_starter.py
358 lines (208 loc) · 6.85 KB
/
Day_14_PfDA_starter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# -*- coding: utf-8 -*-
# <nbformat>3.0</nbformat>
# <markdowncell>
# **Chapter 2, 3 of PDA**
# <codecell>
import matplotlib.pyplot as plt
import numpy as np
from pylab import figure, show
from pandas import DataFrame, Series
import pandas as pd
# <headingcell level=1>
# Preliminaries: Assumed location of pydata-book files
# <markdowncell>
# To make it more practical for me to look at your homework, I'm again going to assume a relative placement of files. I placed the files from
#
# https://github.com/pydata/pydata-book
#
# in a local directory, which in my case is "/Users/raymondyee/D/Document/Working_with_Open_Data/pydata-book/"
#
# and then symbolically linked (`ln -s`) to the the pydata-book from the root directory of the working-open-data folder. i.e., on OS X
#
# cd /Users/raymondyee/D/Document/Working_with_Open_Data/working-open-data
# ln -s /Users/raymondyee/D/Document/Working_with_Open_Data/pydata-book/ pydata-book
#
# That way the files from the pydata-book repository look like they sit in the working-open-data directory -- without having to actually copy the files.
#
# With this arrangment, I should then be able to drop your notebook into my own notebooks directory and run them without having to mess around with paths.
# <codecell>
import os
USAGOV_BITLY_PATH = os.path.join(os.pardir, "pydata-book", "ch02", "usagov_bitly_data2012-03-16-1331923249.txt")
MOVIELENS_DIR = os.path.join(os.pardir, "pydata-book", "ch02", "movielens")
NAMES_DIR = os.path.join(os.pardir, "pydata-book", "ch02", "names")
assert os.path.exists(USAGOV_BITLY_PATH)
assert os.path.exists(MOVIELENS_DIR)
assert os.path.exists(NAMES_DIR)
# <markdowncell>
# **Please make sure the above assertions work**
# <headingcell level=1>
# usa.gov bit.ly example
# <markdowncell>
# (`PfDA`, p. 18)
#
# *What's in the data file?*
#
# <http://my.safaribooksonline.com/book/programming/python/9781449323592/2dot-introductory-examples/id2802197> :
#
# > In 2011, URL shortening service bit.ly partnered with the United States government website usa.gov to provide a feed of anonymous data gathered from users who shorten links ending with .gov or .mil.
#
# Hourly archive of data: <http://bitly.measuredvoice.com/bitly_archive/?C=M;O=D>
# <codecell>
open(USAGOV_BITLY_PATH).readline()
# <codecell>
import json
records = [json.loads(line) for line in open(USAGOV_BITLY_PATH)] # list comprehension
# <headingcell level=2>
# Counting Time Zones with pandas
# <markdowncell>
# Recall what `records` is
# <codecell>
len(records)
# <codecell>
# list of dict -> DataFrame
frame = DataFrame(records)
# <codecell>
frame
# <codecell>
tz_counts = frame['tz'].value_counts()
# <codecell>
tz_counts[:10]
# <codecell>
# fillna
clean_tz = frame['tz'].fillna('Missing')
tz_counts = clean_tz.value_counts()
print tz_counts[:10]
# <codecell>
(clean_tz == '').value_counts()
# <codecell>
# '' -> 'Unknown'
clean_tz[clean_tz == ''] = 'Unknown'
# <codecell>
tz_counts = clean_tz.value_counts()
# <codecell>
tz_counts[:10]
# <codecell>
frame['a'][1]
frame['a'][50]
frame['a'][51]
# <codecell>
tz_counts[:10].plot(kind='barh', rot=0)
# <codecell>
results = Series([x.split()[0] for x in frame.a.dropna()])
# <codecell>
results[:5]
# <codecell>
results.value_counts()[:8]
# <codecell>
frame.a.notnull()
# <codecell>
frame[frame.a.notnull()]
# <codecell>
cframe = frame[frame.a.notnull()]
# <headingcell level=2>
# Let's look at the lat/long in the data
# <markdowncell>
# meaning of other attributes?
#
#
# http://www.usa.gov/About/developer-resources/1usagov.shtml#data
#
#
#
# <codecell>
frame.ll.notnull()
# <headingcell level=2>
# EXERCISE: plot the points represented in frame.ll on a Mercator projected map
# <markdowncell>
# Hints:
#
# * create a naive scatter plot first
# * might want to use `apply` on `Series`
# * look at the Mercator example for Boulder, CA (in Day_14_basemap_redux) -- do the mapping by a loop and then vectorize the operation
# <headingcell level=2>
# Exercise: confirm the value of the earliest and latest of the timestamps -- compute earliest_dt, latest_dt
# <codecell>
frame.t.dropna().apply(datetime.datetime.fromtimestamp)
# <codecell>
# FILL IN
assert earliest_dt == datetime.datetime(2012, 3, 16, 11, 40, 47)
assert latest_dt == datetime.datetime(2012, 3, 16, 12, 40, 49)
# <headingcell level=2>
# Exercise: calculate how often a given net location appears in frame.u
#
# <markdowncell>
# Hints:
#
# * compute `netlocs` as a Series, indexed by Network location part (<http://docs.python.org/2/library/urlparse.html>) of `frame.u`, and holding the number of times that netloc occurs in `frame.u` and sorted in descending order by that number.
# * for full marks, you must use a numpy based approach not a classic Python looping approach
# <codecell>
frame.u
# <codecell>
# FILL IN
# https://github.com/pydata/pandas/issues/240
assert isinstance(netlocs, Series)
assert set(list(netlocs[:5].iteritems())) == set([(u'www.whitehouse.gov', 169),
(u'www.monroecounty.gov', 121),
(u'www.fda.gov', 112),
(u'www.nasa.gov', 733),
(u'www.nysdot.gov', 836)])
# <headingcell level=1>
# Baby names dataset
# <codecell>
import pandas as pd
import codecs
names1880_file = codecs.open(os.path.join(NAMES_DIR,'yob2010.txt'), encoding='iso-8859-1')
names1880 = pd.read_csv(names1880_file, names=['name', 'sex', 'births'])
names1880
# <codecell>
# sort by name
names1880.sort('births', ascending=False)[:10]
# <codecell>
names1880[names1880.sex == 'F'].sort('births', ascending=False)[:10]
# <codecell>
names1880['births'].plot()
# <codecell>
names1880['births'].order(ascending=False).plot()
# <codecell>
names1880['births'].order(ascending=False).cumsum().plot()
# <codecell>
names1880['births'].count()
# <headingcell level=1>
# baby db: straight through working out
# <codecell>
names1880.groupby('sex').births.sum()
# <codecell>
# 2010 is the last available year right now
import os
years = range(1880, 2011)
pieces = []
columns = ['name', 'sex', 'births']
for year in years:
path = os.path.join(NAMES_DIR, 'yob%d.txt' % year)
frame = pd.read_csv(path, names=columns)
frame['year'] = year
pieces.append(frame)
# Concatenate everything into a single DataFrame
names = pd.concat(pieces, ignore_index=True)
# <codecell>
names
# <codecell>
total_births = names.pivot_table('births', rows='year', cols='sex', aggfunc=sum)
# <codecell>
total_births[:5]
# <codecell>
# how to calculate the total births / year?
# <codecell>
# add prop
def add_prop(group):
# Integer division floors
births = group.births.astype(float)
group['prop'] = births / births.sum()
return group
names = names.groupby(['year', 'sex']).apply(add_prop)
# <codecell>
# verify prop
np.allclose(names.groupby(['year', 'sex']).prop.sum(), 1)
# <codecell>
total_births.plot(title='Total births by sex and year')
# <codecell>