Import agate, python based data analysis


In [1]:
import agate

In [33]:
date = agate.Date()

tester = agate.TypeTester(force = {
        'Year': date,
    }, limit=100)
acres = agate.Table.from_csv('irrigatedAcres97-12.csv', column_types=tester)

In [34]:
print(acres)


|-------------------+---------------|
|  column_names     | column_types  |
|-------------------+---------------|
|  Year             | Date          |
|  State            | Text          |
|  State ANSI       | Number        |
|  Ag District      | Text          |
|  Ag District Code | Number        |
|  County           | Text          |
|  County ANSI      | Number        |
|  Commodity        | Text          |
|  Data Item        | Text          |
|  Domain           | Text          |
|  Domain Category  | Text          |
|  Value            | Text          |
|  CV (%)           | Text          |
|-------------------+---------------|


In [35]:
acres.print_table(1)


|-------------+----------+------------+-------------+------------------+---------+-------------+-----------+----------------------+---------------+----------------------+--------+---------|
|        Year | State    | State ANSI | Ag District | Ag District Code | County  | County ANSI | Commodity | Data Item            | Domain        | Domain Category      | Value  | CV (%)  |
|-------------+----------+------------+-------------+------------------+---------+-------------+-----------+----------------------+---------------+----------------------+--------+---------|
|  2012-01-01 | NEBRASKA |         31 | CENTRAL     |               50 | BUFFALO |          19 | AG LAND   | AG LAND, IRRIGATE... | AREA OPERATED | AREA OPERATED: (1... | 71,156 | 24.4    |
|         ... | ...      |        ... | ...         |              ... | ...     |         ... | ...       | ...                  | ...           | ...                  | ...    | ...     |
|-------------+----------+------------+-------------+------------------+---------+-------------+-----------+----------------------+---------------+----------------------+--------+---------|

In [36]:
irrigated_acres = acres.where(lambda row: row['Year'].year == 2012)
print(len(irrigated_acres.rows))


954

The years in the array were chosen due to the USDA ag census was conducted on those dates. Therefore, this program loops through each year in the array, finds the irrigation acres in that year, and chooses the total field in the Domain column. Finally, the counties' total irrigated acres are exported in a csv file. Two counties were removed from the 1997 data because they had no total value for that year.


In [42]:
years = [1997, 2002, 2007, 2012]
for year in years:
    print("Begin {0} irrigated acres analysis".format(year))
    irrigated_acres = acres.where(lambda row: row['Year'].year == year)
    totals = irrigated_acres.where(lambda row: row['Domain'] == 'TOTAL')
    clean_totals = totals.where(lambda row: row['Value'] != '(D)')
    print("Number of counties included {0}".format(len(clean_totals.rows)))
    print_table = clean_totals.select(['Year', 'County', 'Value'])
    irrigation_counts = print_table.rename(column_names = ['Year','County', 'IrrigatedAcres'])
    print_table.print_table(10)
    irrigation_counts.to_csv('irrigatedAcres{0}.csv'.format(year))


Begin 1997 irrigated acres analysis
Number of counties included 91
|-------------+---------+----------|
|        Year | County  | Value    |
|-------------+---------+----------|
|  1997-01-01 | BUFFALO | 213,778  |
|  1997-01-01 | CUSTER  | 197,726  |
|  1997-01-01 | DAWSON  | 222,075  |
|  1997-01-01 | GREELEY | 57,413   |
|  1997-01-01 | HALL    | 181,222  |
|  1997-01-01 | HOWARD  | 111,342  |
|  1997-01-01 | SHERMAN | 59,487   |
|  1997-01-01 | VALLEY  | 73,340   |
|  1997-01-01 | BUTLER  | 97,543   |
|  1997-01-01 | COLFAX  | 53,059   |
|         ... | ...     | ...      |
|-------------+---------+----------|
Begin 2002 irrigated acres analysis
Number of counties included 93
|-------------+---------+----------|
|        Year | County  | Value    |
|-------------+---------+----------|
|  2002-01-01 | BUFFALO | 233,569  |
|  2002-01-01 | CUSTER  | 207,230  |
|  2002-01-01 | DAWSON  | 225,508  |
|  2002-01-01 | GREELEY | 61,405   |
|  2002-01-01 | HALL    | 187,021  |
|  2002-01-01 | HOWARD  | 103,499  |
|  2002-01-01 | SHERMAN | 61,231   |
|  2002-01-01 | VALLEY  | 77,861   |
|  2002-01-01 | BUTLER  | 110,160  |
|  2002-01-01 | CASS    | 2,137    |
|         ... | ...     | ...      |
|-------------+---------+----------|
Begin 2007 irrigated acres analysis
Number of counties included 93
|-------------+---------+----------|
|        Year | County  | Value    |
|-------------+---------+----------|
|  2007-01-01 | BUFFALO | 269,141  |
|  2007-01-01 | CUSTER  | 333,441  |
|  2007-01-01 | DAWSON  | 263,867  |
|  2007-01-01 | GREELEY | 76,393   |
|  2007-01-01 | HALL    | 206,897  |
|  2007-01-01 | HOWARD  | 104,273  |
|  2007-01-01 | SHERMAN | 72,256   |
|  2007-01-01 | VALLEY  | 99,325   |
|  2007-01-01 | BUTLER  | 117,973  |
|  2007-01-01 | CASS    | 2,959    |
|         ... | ...     | ...      |
|-------------+---------+----------|
Begin 2012 irrigated acres analysis
Number of counties included 93
|-------------+---------+----------|
|        Year | County  | Value    |
|-------------+---------+----------|
|  2012-01-01 | BUFFALO | 240,799  |
|  2012-01-01 | CUSTER  | 261,502  |
|  2012-01-01 | DAWSON  | 248,463  |
|  2012-01-01 | GREELEY | 88,190   |
|  2012-01-01 | HALL    | 207,639  |
|  2012-01-01 | HOWARD  | 115,436  |
|  2012-01-01 | SHERMAN | 71,109   |
|  2012-01-01 | VALLEY  | 93,080   |
|  2012-01-01 | BUTLER  | 110,839  |
|  2012-01-01 | CASS    | 3,513    |
|         ... | ...     | ...      |
|-------------+---------+----------|

In [ ]: