-
Notifications
You must be signed in to change notification settings - Fork 0
/
nb_20131006_pew_data.py
453 lines (263 loc) · 8.09 KB
/
nb_20131006_pew_data.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# -*- coding: utf-8 -*-
# <nbformat>2</nbformat>
# <markdowncell>
# # Read the Pew.txt file as analyze some of the data
# <markdowncell>
# We have a Pew.txt file that was exported from the SPSS file from Paul.
#
# See if we can parse it and visualize some of the data.
# <codecell>
import pandas as pd
pd.set_printoptions(max_columns=0)
# <markdowncell>
# Read into dataframe.
# <codecell>
#pew = pd.read_csv('Pew.txt',delimiter=' ', dtype={'fips':object})
#pew = pd.read_csv('Pew.txt',delimiter=' ', dtype={'fips':np.character})
pew = pd.read_csv('Pew.txt',delimiter=' ')
# <codecell>
pew.head(2)
# <markdowncell>
# Some basic summary statistics.
# <codecell>
pew.describe()
# <codecell>
pew.info()
# <markdowncell>
# ## Looking at each column
# <markdowncell>
# ### Dates
# <codecell>
pew.year.hist()
# <markdowncell>
# There are lots of surveys!
# <codecell>
print len(pew.survey.value_counts())
print pew.survey.value_counts().head(2)
print pew.survey.value_counts().tail(2)
# <codecell>
pew[pew.survey=='Heat4B']
# <markdowncell>
# We could parse these *date* strings. They look like mddyy.
# <codecell>
pew.date.value_counts()[:5]
# <markdowncell>
# ### Demographics
# <codecell>
pew.language.value_counts()
# <markdowncell>
# These ages seem strange.
# <codecell>
pew.age.value_counts()[:5]
# <markdowncell>
# There are spikes at regular intervals, e.g., 45 and 50, and at ages with special significance, e.g. 18. Perhaps some surveys were done on people with specific ages, or the people or the poll data collector was rounding down (or up).
# <codecell>
pew.age.hist(bins=100)
# <codecell>
pew.age2.value_counts()
# <codecell>
pew.sex.value_counts()
# <markdowncell>
# Females are *more* likely to answer DK\Refused.
# <codecell>
pew[pew.age2 == 'DK\Refused'].sex.value_counts()
# <codecell>
pew.racethn.value_counts()
# <markdowncell>
# It would be good to have dictionary for these category numbers into a file that could be joined with this Pew.txt file so we know what the values, e.g., *2*, mean.
# <codecell>
pew.hisp.value_counts()
# <codecell>
pew.race.value_counts()
# <codecell>
#pew[pew.race==1].head()
# <markdowncell>
# Why are there two *income* columns?
# <codecell>
pew.income.value_counts()
# <codecell>
pew.income2.value_counts()
# <markdowncell>
# ### Locations
# <codecell>
print pew.fips.value_counts().head()
print pew.fips.value_counts().tail()
#pew.fips.value_counts().plot()
# <codecell>
#pew[['state','fips']][pew.fips.isnull() == False].plot('state','fips')
# <codecell>
#pew[['state','fips',]][pew.fips.isnull() == False].head()
# <codecell>
#pew.info()
# <codecell>
#print pew[pew.fips.isnull() == False].fips.head()
#pew[pew.fips.isnull() == False].fips[122824]
#type(pew[pew.fips.notnull()].fips[122824])
# <markdowncell>
# Why is the *fips* column loading as a float?
# <codecell>
#pew['fips'] = pew.fips.astype(np.character)
# <codecell>
#pew.fips.tail(1)
# <codecell>
#'oops'.rjust(40,'0')
# <codecell>
#pew['FIPS_6-4'] = pew.fips.apply(lambda fips: "%05d"%int(fips))
#pew['FIPS_6-4'] = pew.fips.apply(lambda code: str(code).rjust(5,'0') if type(code)==str else NaN)
#pew.head()
# <codecell>
#pew.info()
# <markdowncell>
# *state* is present whenever *fips* is present. So, just get the county from *fips* whenever it is available.
# <codecell>
print 'state but not fips:', len(pew[pew.state.notnull() & pew.fips.isnull()])
print 'fips but not state:', len(pew[pew.state.isnull() & pew.fips.notnull()])
# <markdowncell>
# Get the FIPS county code from the 5-digit FIPS 6-4 code.
# <codecell>
pew['fips_county'] = pew.fips.apply(lambda code: code%1000 if code != NaN else NaN)
pew[['state','fips','fips_county']][pew.fips_county.notnull()].sort('fips').tail(2)
# <markdowncell>
# ####FIPS
# <markdowncell>
# A separate file with FIPS codes. Use it to get state and county names.
# <codecell>
!head -n 2 data/US_FIPS_Codes.csv
fips = pd.read_csv('data/US_FIPS_Codes.csv')
fips.head(2)
# <codecell>
fips.info()
# <codecell>
#fips['FIPS_6-4'] = fips.apply(lambda row: "%02d%03d"%(row['FIPS State'],row['FIPS County']), axis=1)
#fips.head()
# <markdowncell>
# Get the state names.
# <codecell>
fips_states = fips[['State','FIPS State']].drop_duplicates()
fips_states.head(2)
# <codecell>
pew2 = pew.merge(fips_states, how='left', left_on='state', right_on='FIPS State')
pew2['state_name'] = pew2.State
pew2[pew2.fips.notnull()].sort('fips').head(2)
# <markdowncell>
# Get the county names.
# <codecell>
pew3 = pew2.merge(fips, how='left', left_on=['state','fips_county'], right_on=['FIPS State','FIPS County'])
pew3[['state','fips','fips_county','state_name','County Name']][pew3.fips.notnull()].sort('fips').tail(5)
#pew3[pew3.fips.notnull()].tail(5)
# <codecell>
pew3.head()
# <markdowncell>
# ### ????
# <codecell>
pew.usr.value_counts()
# <markdowncell>
# ### Politics
# <codecell>
pew.party.value_counts()
# <codecell>
pew.partyln.value_counts()
# <markdowncell>
# Fairly well balanced between those leaning Dem and those leaning Rep.
# <codecell>
pew.partysum.value_counts()
# <markdowncell>
# All are registered voters?
# <codecell>
pew.regvoter.value_counts()
# <codecell>
print len(pew.regvoter)
print len(pew[pew.regvoter.isnull()])
# <codecell>
pew[pew.regvoter.isnull()].year.hist(bins=20)
# <codecell>
pew[pew.regvoter.isnull() == False].year.hist(bins=20)
# <codecell>
pew3[pew3['County Name'].notnull()].head()
# <markdowncell>
# Clean up.
# <codecell>
#pew3['FIPS_6-4'] = pew.apply(lambda row: "%02f%03f"%(row['state'],row['fips_county']) if ((row['state']!=NaN) and (row['fips_county']!=NaN)) else NaN,axis=1)
# <codecell>
pew4 = pew3.copy()
del pew4['fips']
#del pew4['state']
#del pew4['fips_county']
del pew4['State_x']
del pew4['FIPS State_x']
del pew4['FIPS State_y']
del pew4['State_y']
del pew4['FIPS County']
pew4 = pew4.rename(columns={'County Name':'county_name',
'fips_county':'fipsco',
'state':'fipsst'})
pew4[pew4.county_name.notnull()].head()
# <markdowncell>
# ## Codebook
# <markdowncell>
# Take a copy of the codes and meanings in CSV form drawn from the Merge Codebook PDF file, and merge its information with the pew data so we can see what the meanings of the values.
# <codecell>
codes = pd.read_csv('data/Merge_Codebook.csv').groupby('column')
codes.head(2)
# <codecell>
code_columns = dict(list(codes))
print code_columns['party'].dtypes
code_columns['party'].head()
# <markdowncell>
# #### Debugging
# <codecell>
column,df_orig = code_columns.items()[1]
df = df_orig.copy()
print column
del df['column']
df = df.rename(columns={'code':column})
print df.dtypes
df
# <codecell>
pew5 = pew4.copy()
pew5 = pew5[['id','rid',column]]
pt = pew5[pew5[column].notnull()][-5:]
#pt[column] = pt[column].astype(object)
pt[column] = pt[column].astype(np.character)
print pt.dtypes
pt
# <codecell>
pt.merge(df, how='left')
# <codecell>
#df = pd.DataFrame({'partyln':['0','1','2','9'], 'meaning':['m0','m1','m2','m9']})
#df
# <markdowncell>
# #### Do the merge.
# <codecell>
pew5 = pew4.copy().sort('id')
for column,df_orig in code_columns.items():
#pew5 = pew4.copy().sort('id')
#pew5 = pew5[['id','rid',column]]
#pew5 = pew5[pew5[column].notnull()][-5:]
df = df_orig.copy()
print "#"*40
print column
del df['column']
df2 = df.rename(columns={'code':column})
print df2
print df2.dtypes
#print "#"*20
#pew5 = pew5.merge(df,how='left', left_on=column, right_on='code')
#print pew5[column].dtype
#print pew5[pew5[column].notnull()].tail()
#print "#"*20
#pew5[column] = pew5[column].astype(object)
pew5[column] = pew5[column].astype(np.character)
#print pew5[column].dtype
#print pew5[pew5[column].notnull()].tail()
print "#"*20
#pew5 = pew5.merge(df2,how='left',on=column)
pew5 = pew5.merge(df2,how='left')
del pew5[column]
#pew5 = pew5.rename(columns={'meaning':column+'_new'})
pew5 = pew5.rename(columns={'meaning':column})
pew5 = pew5.sort('id')
print pew5[pew5[column].notnull()].tail()
# <codecell>
pew5.tail(100)
# <codecell>