-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnb_20131013_pew_data2.py
227 lines (118 loc) · 3.27 KB
/
nb_20131013_pew_data2.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
# -*- coding: utf-8 -*-
# <nbformat>2</nbformat>
# <markdowncell>
# # Analyze the Pew.txt file
# <markdowncell>
# This time using separate function to read in the file.
# <codecell>
import pandas as pd
%load_ext autoreload
%autoreload 2
from pew_data_analysis import *
pd.set_printoptions(max_columns=0)
# <codecell>
%time pew = read_pew_txt('Pew.txt','data/Merge_Codebook.csv','data/US_FIPS_Codes.csv')
# <codecell>
pew.describe()
# <codecell>
pew.head(2)
# <markdowncell>
# ## Analyze 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)
# <markdowncell>
# We could parse these *date* strings. They look like mddyy.
# <codecell>
pew.date.value_counts()[:5]
# <codecell>
#pew['date_new'] = pew.date.apply(lambda d:
# <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()
# <codecell>
pew.hisp.value_counts()
# <codecell>
pew.race.value_counts()
# <markdowncell>
# Why are there two *income* columns?
# <codecell>
pew.income.value_counts()
# <codecell>
pew.income2.value_counts()
# <codecell>
pew.educ.value_counts()
# <codecell>
pew.head(2)
# <markdowncell>
# ### Locations
# <markdowncell>
# States, then counties, with most and least responses.
# <codecell>
print pew.state_name.value_counts()[:3]
print pew.state_name.value_counts()[-3:]
# <codecell>
print pew.county_name.value_counts()[:3]
print pew.county_name.value_counts()[-3:]
# <markdowncell>
# Mostly suburbanites.
# <codecell>
pew.usr.value_counts()
# <markdowncell>
# According to the Merge Codebook document, the scale goes from lowest population densith (1) to greatest (5). Not sure what a *9* indicates; no data?
# <codecell>
pew.density.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()])
# <markdowncell>
# The number of responses with *regvoter* seems to be increasing over the years compared to the number with *regvoter* missing.
# <codecell>
pew[pew.regvoter.isnull()].year.hist(bins=20)
# <codecell>
pew[pew.regvoter.notnull()].year.hist(bins=20)
# <markdowncell>
# ## Write to CSV
# <codecell>
%time pew.to_csv('data/Pew_for_analysis.csv', index=False)
# <codecell>