forked from wireservice/agate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
executable file
·68 lines (48 loc) · 1.69 KB
/
example.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
#!/usr/bin/env python
import csv
from journalism import Table, TextType, NumberType
text_type = TextType()
number_type = NumberType()
COLUMNS = (
('state', text_type),
('state_abbr', text_type),
('9_11_gi_bill1', number_type),
('montogomery_gi_bill_active', number_type),
('montgomery_gi_bill_reserve', number_type),
('dependants', number_type),
('reserve', number_type),
('vietnam', number_type),
('total', number_type)
)
COLUMN_NAMES = tuple(c[0] for c in COLUMNS)
COLUMN_TYPES = tuple(c[1] for c in COLUMNS)
with open('examples/realdata/Datagov_FY10_EDU_recp_by_State.csv') as f:
# Skip headers
next(f)
next(f)
next(f)
rows = list(csv.reader(f))
# Trim cruft off end
rows = rows[:-2]
# Create the table
table = Table(rows, COLUMN_TYPES, COLUMN_NAMES)
# Remove Phillipines and Puerto Rico
states = table.where(lambda r: r['state_abbr'] not in ('PR', 'PH'))
# Sum total of all states
print('Total of all states: %i' % states.columns['total'].sum())
# Sort state total, descending
order_by_total_desc = states.order_by('total', reverse=True)
# Grab just the top 5 states
top_five = order_by_total_desc.rows[:5]
for i, row in enumerate(top_five):
print('# %i: %s %i' % (i, row['state'], row['total']))
with open('sorted.csv', 'w') as f:
writer = csv.writer(f)
writer.writerow(order_by_total_desc.get_column_names())
writer.writerows(order_by_total_desc.rows)
# Grab just the bottom state
last_place = order_by_total_desc.rows[-1]
print('Lowest state: %(state)s %(total)i' % last_place)
# Calculate the standard of deviation for the state totals
stdev = states.columns['total'].stdev()
print('Standard deviation of totals: %.2f' % stdev)