<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Using agate in a Jupyter notebook
First we import agate. Then we create an agate Table by loading data from a CSV file.
Step1:  Question 1
Step2:  We can then print the Sum of the costs of all those rows. (The cost column is named total_cost.)
Step3:  Question 2
Step4:  We then use the aggregate function to sum the total_cost column for each table in the group. The resulting values are collapsed into a new table, totals, which has a row for each county and a column named total_cost_sum containing the new total.
Step5:  Finally, we sort the counties by their total cost, limit the results to the top 10 and then print the results as a text bar chart.
<ASSISTANT_TASK:>
Python Code:
import agate
table = agate.Table.from_csv('examples/realdata/ks_1033_data.csv')
table
Explanation: Using agate in a Jupyter notebook
First we import agate. Then we create an agate Table by loading data from a CSV file.
End of explanation
kansas_city = table.where(lambda r: r['county'] in ('JACKSON', 'CLAY', 'CASS', 'PLATTE'))
print(len(table.rows))
print(len(kansas_city.rows))
Explanation: Question 1: What was the total cost to Kansas City area counties?
To answer this question, we first must filter the table to only those rows which refer to a Kansas City area county.
End of explanation
print('$%d' % kansas_city.aggregate(agate.Sum('total_cost')))
Explanation: We can then print the Sum of the costs of all those rows. (The cost column is named total_cost.)
End of explanation
# Group by county
counties = table.group_by('county')
print(counties.keys())
Explanation: Question 2: Which counties spent the most?
This question is more complicated. First we group the data by county, which gives us a TableSet named counties. A TableSet is a group of tables with the same columns.
End of explanation
# Aggregate totals for all counties
totals = counties.aggregate([
    ('total_cost_sum', agate.Sum('total_cost'),)
])
print(totals.column_names)
Explanation: We then use the aggregate function to sum the total_cost column for each table in the group. The resulting values are collapsed into a new table, totals, which has a row for each county and a column named total_cost_sum containing the new total.
End of explanation
totals.order_by('total_cost_sum', reverse=True).limit(20).print_bars('county', 'total_cost_sum', width=100)
Explanation: Finally, we sort the counties by their total cost, limit the results to the top 10 and then print the results as a text bar chart.
End of explanation
<END_TASK>