Contact Us!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
veeralakrishna

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: veeralakrishna/DataCamp-Project-Solutions-Python
Path: blob/master/Who Is Drunk and When in Ames, Iowa_/notebook.ipynb
Views: 1244
Kernel: Python 3

1. Breath alcohol tests in Ames, Iowa, USA

Ames, Iowa, USA is the home of Iowa State University, a land grant university with over 36,000 students. By comparison, the city of Ames, Iowa, itself only has about 65,000 residents. As with any other college town, Ames has had its fair share of alcohol-related incidents. (For example, Google 'VEISHEA riots 2014'.) We will take a look at some breath alcohol test data from Ames that is published by the State of Iowa.

The data file 'breath_alcohol_ames.csv' contains 1,556 readings from breath alcohol tests administered by the Ames and Iowa State University Police Departments from January 2013 to December 2017. The columns in this data set are year, month, day, hour, location, gender, Res1, Res2.

# import pandas # ... YOUR CODE FOR TASK 1 ... import pandas as pd # read the data into your workspace ba_data = pd.read_csv('datasets/breath_alcohol_ames.csv') # quickly inspect the data print(ba_data.head()) # obtain counts for each year ba_year = ba_data['year'].value_counts()
year month day hour location gender Res1 Res2 0 2017 12 17 1 Ames PD M 0.046 0.046 1 2017 12 14 3 ISU PD F 0.121 0.120 2 2017 12 10 5 ISU PD F 0.068 0.067 3 2017 12 10 3 ISU PD F 0.077 0.077 4 2017 12 9 2 ISU PD M 0.085 0.084

2. What is the busiest police department in Ames?

There are two police departments in the data set: the Iowa State University Police Department and the Ames Police Department. Which one administers more breathalyzer tests?

# use value_counts to tally up the totals for each department pds = ba_data['location'].value_counts() pds
ISU PD 940 Ames PD 616 Name: location, dtype: int64

3. Nothing Good Happens after 2am

We all know that "nothing good happens after 2am." Thus, there are inevitably some times of the day when breath alcohol tests, especially in a college town like Ames, are most and least common. Which hours of the day have the most and least breathalyzer tests?

%matplotlib inline # count by hour hourly = ba_data.groupby(['hour']).size() # create a vertical bar graph of the arrest count by hour hourly.plot.bar(x=hourly)
<matplotlib.axes._subplots.AxesSubplot at 0x7ff3975ca748>
Image in a Jupyter notebook

4. Breathalyzer tests by month

Now that we have discovered which time of day is most common for breath alcohol tests, we will determine which time of the year has the most breathalyzer tests. Which month will have the most recorded tests?

# count by month and arrange by descending frequency monthly = ba_data.groupby(['month']).size() # use plot.bar to make the appropriate bar chart monthly.plot.bar(x='monthly')
<matplotlib.axes._subplots.AxesSubplot at 0x7ff3976ef630>
Image in a Jupyter notebook

5. COLLEGE

When we think of (binge) drinking in college towns in America, we usually think of something like this image at the left. And so, one might suspect that breath alcohol tests are given to men more often than women and that men drink more than women.

# count by gender counts_gender = ba_data['gender'].value_counts() # create a dataset with no NAs in gender gen = ba_data.dropna(subset=['gender']) # create a mean test result variable mean_bas = gen.assign(meanRes=(gen.Res1+gen.Res2)/2) # # create side-by-side boxplots to compare the mean blood alcohol levels of men and women mean_bas.boxplot(['meanRes'], by='gender')
<matplotlib.axes._subplots.AxesSubplot at 0x7ff3974cb5c0>
Image in a Jupyter notebook

In the USA, it is illegal to drive with a blood alcohol concentration (BAC) above 0.08%. This is the case for all 50 states. Assuming everyone tested in our data was driving (though we have no way of knowing this from the data), if either of the results (Res1, Res2) are above 0.08, the person would be charged with DUI (driving under the influence).

# Filter the data duis = ba_data[(ba_data.Res1 > 0.08) | (ba_data.Res2 > 0.08)] # proportion of tests that would have resulted in a DUI p_dui = duis.shape[0] / ba_data.shape[0] p_dui
0.7448586118251928

7. Breathalyzer tests: is there a pattern over time?

We previously saw that 2am is the most common time of day for breathalyzer tests to be administered, and August is the most common month of the year for breathalyzer tests. Now, we look at the weeks in the year over time.

# Create date variable ba_data['date'] = pd.to_datetime(ba_data[['year', 'month', 'day']]) # Create a week variable ba_data['week'] = ba_data['date'].dt.week # Check your work ba_data.head()

8. Looking at timelines

How do the weeks differ over time? One of the most common data visualizations is the time series, a line tracking the changes in a variable over time. We will use the new week variable to look at test frequency over time. We end with a time series plot showing the frequency of breathalyzer tests by week in year, with one line for each year.

# choose and count the variables of interest timeline = ba_data.groupby(['week','year']).count()['Res1'] # unstack and plot timeline.unstack().plot(title='VEISHEA DUIs', legend=True)
<matplotlib.axes._subplots.AxesSubplot at 0x7ff397a24fd0>
Image in a Jupyter notebook

9. The end of VEISHEA

From Wikipedia: "VEISHEA was an annual week-long celebration held each spring on the campus of Iowa State University in Ames, Iowa. The celebration featured an annual parade and many open-house demonstrations of the university facilities and departments. Campus organizations exhibited products, technologies, and held fundraisers for various charity groups. In addition, VEISHEA brought speakers, lecturers, and entertainers to Iowa State. [...] VEISHEA was the largest student-run festival in the nation, bringing in tens of thousands of visitors to the campus each year."

This over 90-year tradition in Ames was terminated permanently after riots in 2014, where drunk celebrators flipped over multiple vehicles and tore light poles down. This was not the first incidence of violence and severe property damage in VEISHEA's history. Did former President Leath make the right decision by canceling VEISHEA?

## Was it right to permanently cancel VEISHEA? TRUE or FALSE? canceling_VEISHEA_was_right = True