download notebook
view notebook w/ solutions

Exam 1 Fall 2023 (100 pts total)

files needed = ('phil.csv', 'tax.csv', 'imports.xlsx', 'data.xlsx'), which can be found in exam1_data.zip

You have 75 minutes to complete this exam.

Answer all the questions below in this notebook. You should insert as many cells into the notebook as you need. When you are finished, upload your finished notebook to Canvas.

  • You may use your notes and the Internet.
  • You cannot work with others on the exam. You cannot post questions online and solicit answers, e.g., through Chegg or chat GPT.
  • Import any packages you need to complete this exam.
  • Do not modify the data files directly. All data manipulation should happen in your code.

Question 0 (5 points): Last, First

Replace 'Last, First' above with your actual name. Enter it as: last name, first name.

Question 1 (30 points): Plotting

The file 'phil.csv' contains annual data on the average unemployment rate (UNRATE) and the CPI inflation rate (INFL). These are components of the Phillips Curve.

  1. Create a histogram of the unemployment rate data.
  2. The histogram's color should be 'silver'.
  3. The histogram should have 15 bins.
  4. The figure size should be 8 inches wide and 5 inches tall.
  5. The tick labels on the y-axis should be integers—there should be not digits to the right of the decimal point.

Make any further adjustments you find necessary.

Question 2 (5 points): Subsets of DataFrames

  1. Create a dataframe from the file "tax.csv".
  2. How many entries were built in 2022? The variable YearBuilt hold the build date. Print it as:
There are ??? buildings built in 2022.

Replace the ??? with your answer.

Question 3 (20 points): Working with DataFrames

The file 'imports.xlsx' contains two variables. imports china is the value of US imports from China from 1985–2022 and imports all is the value of total US imports from 1985–2022.

Use the data to answer the following questions.

  1. Compute the share of Chinese imports in total imports, i.e., imports china / imports all * 100

  2. The China-US trade war intensified in 2018. Print out China's share of US imports as follows:

China's share in US trade was ?? percent in 2018 and ?? percent in 2022.

Replace the ??s with your answers. The two numbers should be expressed as integers.

Question 4 (5 points): Working with indexes

  1. Load the file 'data.xlsx' into a DataFrame. Name the DataFrame df1.
  2. Create a DataFrame named df2 that has only the columns v2, v12, v15, and v22.
  3. Set the index of df1 to be column v15.
  4. Print out the first 3 rows of df2.

Question 5 (20 points): Dictionaries and functions

Write a function (call it q5) that takes a dict as an argument. The dict holds the the quantity of lumber and species of the lumber. The function computes and returns the total value of the lumber in the dict, that is, the quantity times the price. Oak sells for \\(2.50 each and cherry sells for \\\)6.25 each.

Test your function by running the following code (copy and past it into a code cell):

tree1 = {'quantity':10, 'species':'oak'}
tree2 = {'quantity':25, 'species':'cherry'}
print(q5(tree1))
print(q5(tree2))

Question 6 (5 points): Dataframes

Consider the code below when answering the following questions.

df = pd.DataFrame({'a':range(0,10), 'b':range(5,15), 'c':range(-2,17, 2)})

for i in range(0, 10):
    df.loc[i, 'd'] = (df.loc[i, 'a'] + df.loc[i, 'b'] + df.loc[i, 'c'])/3

print(df)

You do not need to write any code to answer these question. Insert a markdown cell and type your answer out.

  1. What does this code do?
  2. Why is this code a poor way to accomplish this task?

Question 7 (10 points):

Write code that removes the vowels (a, e, i, o, u) from the following string. Print out the "devoweled" string.

ozy = 'Look on my works, ye Mighty, and despair!'

You are finished!

Upload your completed notebook to Canvas.