Read States

The questions below are due on Sunday July 07, 2024; 10:00:00 PM.
 
You are not logged in.

Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.

1) Preparation

The following file contains code and other resources as a starting point for this assignment: ZIP FOLDER

2) Introduction

You have been recently appointed as the Chief Trade Officer (CTO) of the U.S. Commerce Department. Your job as CTO is to organize all of the trade transactions between different states and determine which states have the most exports.

To accomplish this task you are given a database of real U.S. trade data1 represented as a list of lists of the following form

['ORIGIN_STATE', 'DESTINATION_STATE', 'COMMODITY', 'MODE', 'VALUE']

For example, the transactions in our tiny_transaction database records show:

[['MA', 'CA', 'Grains', 'Truck', 47097],
 ['MA', 'ND', 'Wood', 'Truck', 12781],
 ['ND', 'MA', 'Grains', 'Truck', 12087],
 ['WY', 'ND', 'Fuels', 'Rail', 128633],
 ['ND', 'SC', 'Grains', 'Truck', 1456],
 ['SC', 'MT', 'Wood', 'Air', 41],
 ['MA', 'MA', 'Wood', 'Truck', 10606],
 ['MT', 'WY', 'Fuels', 'Truck', 584],
 ['CA', 'MA', 'Grains', 'Truck', 16463],
 ['CA', 'ND', 'Grains', 'Truck', 12462],
 ['ND', 'CA', 'Wood', 'Truck', 23],
 ['MT', 'ND', 'Grains', 'Truck', 23690],
 ['CA', 'MA', 'Wood', 'Truck', 1181],
]

3) Reading the Database

For your first exercise, fill in the body of the load_database function. While this function will not be officially tested, it will help you answer the later questions and test your code. It should be pretty similar to the example provided in the reading. If you load and print the tiny_transaction.csv, you should get the same output as shown above in the previous section.

4) Unique Destinations

Now that we can load the databases, fill in the body of the unique_destinations function.

By looking at the tiny database, how many unique destination states does MA have (including itself!)?

How many unique destination states does NM have in the large database?

Note: you can test this function by running the provided test.py file. Using the test.py file on your computer is optional, but will hopefully make debugging easier than parsing through the tests on the website. After your unique_destinations function is implemented correctly, it should pass the first test case.

Upload your code here to test your unique_destinations function
  No file selected

5) Transforming the Data

Now that we have thought about different ways to visually represent the database, how can we use Python to simplify our database to just represent the origin and the values of the various transactions? Fill in the body of transform_dict function to do just that!

Looking at the tiny database, what is the total value of the exports of MA (not including transactions where MA is the destination)?

Which state has the greatest total value of the exports in the large database? (Enter your answer as a string)

Note: you can test your transform_dict function by running the test.py file on your computer as described earlier.

Upload your code here to test your transform_dict function
  No file selected

In the next exercise, you'll get the opportunity to turn the data we have processed into a new spreadsheet that summarizes the export value data!

Next Exercise: Write States

Back to exercises


 
Footnotes

1The databases we use in this assignment are a processed subset of the 2012 Commodity Flow Survey Dataset published by the U.S. Census Bureau. (click to return to text)