<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Playing around with Pittsburgh 311 Data
This notebook is going to play around with the 311 Data from the Western Pennsylvania Regional Data Center
I have taken the liberty of downloading the 311 data
Step1:  Embedded Plots
Step2:  Exploring Request types
Step3:  There are too many request types (268). We need some higher level categories to make this more comprehensible. Fortunately, there is an Issue and Category codebook that we can use to map between low and higher level categories.
Step4:  That is a more manageable list of categories for data visualization. Let's take a look at the distribution of requests per category in the dataset.
Step5:  Looking at requests at the neighborhood level
Thankfully, the 311 data from the WPRDC already includes neighborhood information for each request in the NEIGHBORHOOD column. We can take advantage of this to filter and count requests by neighborhood.
Step6:  In GRAPH form
Step9:  So we can see from the graph above that Brookline, followed by the South Side Slopes, Carrick, and South Side Flats, make the most 311 requests. It would be interesting to get some neighborhood population data and compute the number of requests per capita. 
I bet those data are available, maybe YOU could create that graph!
Widgets
Jupyter Notebooks have a very powerful widget framework that allows you to easily add interactive components to live notebooks.
<ASSISTANT_TASK:>
Python Code:
# use the %ls magic to list the files in the current directory.
%ls
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sms
%matplotlib inline
three11s = pd.read_csv("data/pgh-311.csv", parse_dates=['CREATED_ON'])
three11s.dtypes
three11s.head()
three11s.loc[0]
Explanation: Playing around with Pittsburgh 311 Data
This notebook is going to play around with the 311 Data from the Western Pennsylvania Regional Data Center
I have taken the liberty of downloading the 311 data
End of explanation
# Plot the number of 311 requests per month
month_counts = three11s.groupby(three11s.CREATED_ON.dt.month)
y = month_counts.size()
x = month_counts.CREATED_ON.first()
axes = pd.Series(y.values, index=x).plot(figsize=(15,5))
plt.ylim(0)
plt.xlabel('Month')
plt.ylabel('Complaint')
Explanation: Embedded Plots
End of explanation
grouped_by_type = three11s.groupby(three11s.REQUEST_TYPE)
size = grouped_by_type.size()
size
#len(size)
#size[size > 200]
Explanation: Exploring Request types
End of explanation
codebook = pd.read_csv('data/codebook.csv')
codebook.head()
merged_data = pd.merge(three11s, 
                       codebook[['Category', 'Issue']], 
                       how='left',
                       left_on="REQUEST_TYPE", 
                       right_on="Issue")
merged_data.head()
grouped_by_type = merged_data.groupby(merged_data.Category)
size = grouped_by_type.size()
size
Explanation: There are too many request types (268). We need some higher level categories to make this more comprehensible. Fortunately, there is an Issue and Category codebook that we can use to map between low and higher level categories.
End of explanation
size.plot(kind='barh', figsize=(8,6))
Explanation: That is a more manageable list of categories for data visualization. Let's take a look at the distribution of requests per category in the dataset.
End of explanation
merged_data.groupby(merged_data.NEIGHBORHOOD).size().sort_values(inplace=False,
                                                         ascending=False)
Explanation: Looking at requests at the neighborhood level
Thankfully, the 311 data from the WPRDC already includes neighborhood information for each request in the NEIGHBORHOOD column. We can take advantage of this to filter and count requests by neighborhood.
End of explanation
merged_data.groupby(merged_data.NEIGHBORHOOD).size().sort_values(inplace=False,
                                                         ascending=True).plot(kind="barh", figsize=(5,20))
Explanation: In GRAPH form
End of explanation
# create a function that generates a chart of requests per neighborhood
def issues_by_neighborhood(neighborhood):
    Generates a plot of issue categories by neighborhood
    grouped_by_type = merged_data[merged_data['NEIGHBORHOOD'] == neighborhood].groupby(merged_data.Category)
    size = grouped_by_type.size()
    size.plot(kind='barh', figsize=(8,6))
issues_by_neighborhood('Greenfield')
issues_by_neighborhood('Brookline')
issues_by_neighborhood('Garfield')
from ipywidgets import interact
@interact(hood=sorted(list(pd.Series(three11s.NEIGHBORHOOD.unique()).dropna())))
def issues_by_neighborhood(hood):
    Generates a plot of issue categories by neighborhood
    grouped_by_type = merged_data[merged_data['NEIGHBORHOOD'] == hood].groupby(merged_data.Category)
    size = grouped_by_type.size()
    size.plot(kind='barh',figsize=(8,6))
Explanation: So we can see from the graph above that Brookline, followed by the South Side Slopes, Carrick, and South Side Flats, make the most 311 requests. It would be interesting to get some neighborhood population data and compute the number of requests per capita. 
I bet those data are available, maybe YOU could create that graph!
Widgets
Jupyter Notebooks have a very powerful widget framework that allows you to easily add interactive components to live notebooks.
End of explanation
<END_TASK>