Exemplo n.º 1
0
with open('examples/realdata/Datagov_FY10_EDU_recp_by_State.csv') as f:
    # Skip headers
    f.next()
    f.next()
    f.next()

    rows = list(csv.reader(f))

# Trim cruft off end
rows = rows[:-2]

# Create the table
table = Table(rows, COLUMN_TYPES, COLUMN_NAMES, cast=True)

# Remove Phillipines and Puerto Rico
states = table.reject(lambda r: r['state_abbr'] in ['PR', 'PH'])

# Sum total of all states
print 'Total of all states: %i' % states.columns['total'].sum()

# Sort state total, descending
sort_by_total_desc = states.sort_by('total', reverse=True)

# Grab just the top 5 states
top_five = sort_by_total_desc.rows[0:5]

for i, row in enumerate(top_five):
    print '# %i: %s %i' % (i, row['state'], row['total'])

# Grab just the bottom state
last_place = sort_by_total_desc.rows[-1]
Exemplo n.º 2
0
with open('examples/realdata/Datagov_FY10_EDU_recp_by_State.csv') as f:
    # Skip headers
    f.next()
    f.next()
    f.next()

    rows = list(csv.reader(f))

# Trim cruft off end
rows = rows[:-2]

# Create the table
table = Table(rows, COLUMN_TYPES, COLUMN_NAMES, cast=True)

# Remove Phillipines and Puerto Rico
states = table.reject(lambda r: r['state_abbr'] in ['PR', 'PH'])

# Sum total of all states
print 'Total of all states: %i' % states.columns['total'].sum()

# Sort state total, descending
sort_by_total_desc = states.sort_by('total', reverse=True)

# Grab just the top 5 states
top_five = sort_by_total_desc.rows[0:5]

for i, row in enumerate(top_five):
    print '# %i: %s %i' % (i, row['state'], row['total'])

# Grab just the bottom state
last_place = sort_by_total_desc.rows[-1]