In [64]:
import plotly
import plotly.tools as tls
import cufflinks as cf
import pandas as pd
import matplotlib.pyplot as plt

In [65]:
username = 'phillipwong'
api_key  = 'jSZgLHfjjQcjQTnsWzhE'

plotly.tools.set_credentials_file(username=username, api_key=api_key)

In [66]:
cols = ['Week Number', 'RequestType', 'Created Year', 'CD']
df = pd.read_csv('311_no_empty_coordinates.csv', usecols = cols)

In [67]:
df['CD'] = pd.to_numeric(df['CD'], errors='coerce')

In [31]:
df['RequestType'].describe()


Out[31]:
count         2903344
unique             12
top       Bulky Items
freq          1351017
Name: RequestType, dtype: object

In [155]:
x = df.loc[df['RequestType'] == 'Bulky Items']
x = df.loc[df['Created Year'] == 2015]

In [150]:
group = df.groupby(['RequestType','Week Number']).size().unstack()
group


Out[150]:
Week Number 1 2 3 4 5 6 7 8 9 10 ... 44 45 46 47 48 49 50 51 52 53
RequestType
Bulky Items 23681.0 24112.0 24755.0 26130.0 26350.0 24769.0 21501.0 23091.0 26526.0 26362.0 ... 22595.0 23040.0 20702.0 12913.0 16553.0 25430.0 25401.0 21486.0 18686.0 5483.0
Dead Animal Removal 1278.0 1374.0 1380.0 1315.0 1231.0 1256.0 1183.0 1000.0 1161.0 1122.0 ... 1276.0 1313.0 1074.0 734.0 892.0 1226.0 1263.0 1012.0 983.0 365.0
Electronic Waste 2383.0 2423.0 2258.0 2485.0 2455.0 2265.0 2122.0 2259.0 2456.0 2271.0 ... 1986.0 2152.0 2023.0 1454.0 1833.0 2753.0 2718.0 2303.0 1968.0 740.0
Feedback 24.0 31.0 18.0 32.0 19.0 32.0 33.0 40.0 32.0 21.0 ... 18.0 21.0 14.0 14.0 16.0 14.0 22.0 17.0 16.0 2.0
Graffiti Removal 12668.0 17892.0 18593.0 18292.0 20604.0 20862.0 18859.0 20375.0 20784.0 21451.0 ... 11654.0 11264.0 9869.0 3947.0 7972.0 12018.0 12144.0 11740.0 11371.0 NaN
Homeless Encampment 1045.0 1026.0 1165.0 1156.0 1070.0 1109.0 888.0 884.0 1174.0 1315.0 ... 851.0 854.0 691.0 394.0 621.0 766.0 815.0 598.0 621.0 50.0
Illegal Dumping Pickup 4230.0 4113.0 4145.0 4004.0 4037.0 3851.0 3408.0 3422.0 3642.0 3578.0 ... 3623.0 3827.0 3164.0 1719.0 2223.0 3706.0 3459.0 3048.0 2669.0 710.0
Metal/Household Appliances 3519.0 3483.0 3446.0 3645.0 3752.0 3561.0 3421.0 3491.0 3858.0 3957.0 ... 3316.0 3283.0 3149.0 1887.0 2514.0 3818.0 3673.0 3299.0 2920.0 830.0
Multiple Streetlight Issue 294.0 350.0 367.0 374.0 291.0 283.0 276.0 288.0 282.0 255.0 ... 225.0 265.0 233.0 164.0 233.0 290.0 306.0 271.0 232.0 55.0
Other 929.0 1021.0 1030.0 1041.0 993.0 945.0 989.0 936.0 947.0 873.0 ... 607.0 534.0 471.0 204.0 379.0 559.0 516.0 464.0 471.0 3.0
Report Water Waste 54.0 50.0 59.0 57.0 72.0 77.0 85.0 62.0 70.0 71.0 ... 66.0 62.0 52.0 26.0 41.0 56.0 72.0 47.0 59.0 3.0
Single Streetlight Issue 662.0 646.0 708.0 842.0 762.0 643.0 789.0 807.0 743.0 660.0 ... 720.0 759.0 758.0 581.0 460.0 647.0 903.0 722.0 536.0 138.0

12 rows × 53 columns


In [190]:
#NOTE: this encompasses all data points from all years. I need to filter this according to year so I see how it changes within the year and is specific to one week
#Input which week you'd like to see how request types changed for
#Ideally would be able to create a column that analyzes how the last week changed
#as well as find a way to isolate how specific types changed
#but this allows me to see how they all changed from the week before for every week of the year

#group CSV by Request Type and Week Number
group = df.groupby(['RequestType','Week Number']).size().unstack()

#User input to determine what week to see how requests changed
x = df['Week Number'].max()
group['diff'] = group[x] - group [x-1]

#Plotting how each type changed over the week 
group['diff'].iplot(kind = 'bar', yTitle='Change in Number of Requests from Previous Week', title='LA 311 Week by Week Comparisons: 2015-2018',
             filename='WeeklyComparison')


Out[190]:

In [193]:
df_2015 = df.loc[df['Created Year'] == 2015]

#group CSV by Request Type and Week Number
group_one= df_2015.groupby(['RequestType','Week Number']).size().unstack()

#Determining the latest week in 2016 for which there is data
#then calculating the difference in number of each request type 
#between that week and the previous week
x = df_2015['Week Number'].max()
group_one['diff'] = group_one[x] - group_one[x-1]


#Plotting how each type changed over the week 
group_one['diff'].iplot(kind = 'bar', yTitle='Change in Number of Requests from Previous Week', title='LA 311 Week by Week Comparisons: 2015',
             filename='LA311WeeklyComparison: 2015')


Out[193]:

In [162]:
df_2016 = df.loc[df['Created Year'] == 2016]

#group CSV by Request Type and Week Number
group_two= df_2016.groupby(['RequestType','Week Number']).size().unstack()

#Determining the latest week in 2016 for which there is data
#then calculating the difference in number of each request type 
#between that week and the previous week
x = df_2016['Week Number'].max()
group_two['diff'] = group_two[x] - group_two[x-1]

#Plotting how each type changed over the week 
group_two['diff'].iplot(kind = 'bar', yTitle='Change in Number of Requests from Previous Week', title='LA 311 Week by Week Comparisons: 2016',
             filename='LA311WeeklyComparison2016')


week number:23
Out[162]:

In [194]:
df_2017 = df.loc[df['Created Year'] == 2017]

#group CSV by Request Type and Week Number
group_three = df_2017.groupby(['RequestType','Week Number']).size().unstack()

#Determining the latest week in 2016 for which there is data
#then calculating the difference in number of each request type 
#between that week and the previous week
x = df_2017['Week Number'].max()
group_three['diff'] = group_three[x] - group_three[x-1]

#Plotting how each type changed over the week 
group_three['diff'].iplot(kind = 'bar', yTitle='Change in Number of Requests from Previous Week', title='LA 311 Week by Week Comparisons: 2017',
             filename='LA311WeeklyComparison2017')


Out[194]:

In [68]:
df_2018 = df.loc[df['Created Year'] == 2018]

#group CSV by Request Type and Week Number
group_four= df_2018.groupby(['RequestType','Week Number']).size().unstack()

#Determining the latest week in 2016 for which there is data
#then calculating the difference in number of each request type 
#between that week and the previous week
x = df_2018['Week Number'].max()
group_four['diff'] = group_four[x] - group_four[x-1]

#Plotting how each type changed over the week 
group_four['diff'].iplot(kind = 'bar', yTitle='Change in Number of Requests from Previous Week', title='LA 311 Week by Week Comparisons: 2018',
             filename='LA311WeeklyComparison2018')


/anaconda3/lib/python3.6/site-packages/cufflinks/plotlytools.py:156: FutureWarning:

pandas.tslib is deprecated and will be removed in a future version.
You can access Timestamp as pandas.Timestamp

Out[68]:

In [223]:
df_2018_7plus = df.loc[df['CD'] == 1.0]

#group CSV by Request Type and Week Number
cds_7plus= df_2018.groupby(['RequestType','Week Number']).size().unstack()
cds_7plus


Out[223]:
Week Number 1 2 3 4 5 6 7 8 9 10 ... 21 22 23 24 25 26 27 28 29 30
RequestType
Bulky Items 9293.0 9471.0 11032.0 10036.0 10104.0 10788.0 7845.0 7729.0 9467.0 9823.0 ... 10221.0 10783.0 11756.0 11555.0 11551.0 12340.0 11285.0 12398.0 13363.0 750.0
Dead Animal Removal 450.0 481.0 516.0 469.0 458.0 410.0 442.0 274.0 381.0 355.0 ... 585.0 532.0 559.0 613.0 568.0 597.0 590.0 654.0 615.0 26.0
Electronic Waste 772.0 816.0 793.0 877.0 788.0 891.0 678.0 680.0 754.0 738.0 ... 746.0 743.0 823.0 834.0 838.0 815.0 734.0 884.0 919.0 44.0
Feedback 7.0 13.0 5.0 11.0 6.0 11.0 18.0 21.0 9.0 4.0 ... 12.0 13.0 13.0 48.0 57.0 5.0 7.0 7.0 10.0 NaN
Graffiti Removal 4384.0 6369.0 6119.0 6733.0 6820.0 7235.0 7725.0 6538.0 6403.0 8409.0 ... 6024.0 5110.0 5684.0 5847.0 6175.0 5990.0 4500.0 6266.0 5565.0 400.0
Homeless Encampment 584.0 574.0 679.0 669.0 602.0 631.0 443.0 430.0 615.0 753.0 ... 764.0 728.0 864.0 988.0 953.0 828.0 627.0 845.0 790.0 38.0
Illegal Dumping Pickup 1956.0 1872.0 2022.0 1884.0 1786.0 1810.0 1557.0 1533.0 1480.0 1578.0 ... 1802.0 1806.0 1851.0 2049.0 1956.0 1981.0 1746.0 2183.0 2381.0 114.0
Metal/Household Appliances 1454.0 1436.0 1538.0 1505.0 1532.0 1549.0 1377.0 1232.0 1425.0 1513.0 ... 1583.0 1655.0 1890.0 1767.0 1885.0 1944.0 1706.0 1852.0 1953.0 89.0
Multiple Streetlight Issue 80.0 130.0 170.0 158.0 97.0 109.0 91.0 84.0 92.0 94.0 ... 49.0 52.0 59.0 34.0 45.0 76.0 88.0 81.0 79.0 7.0
Other 403.0 402.0 363.0 297.0 316.0 325.0 304.0 253.0 278.0 273.0 ... 338.0 315.0 311.0 373.0 371.0 362.0 384.0 317.0 294.0 13.0
Report Water Waste 35.0 21.0 18.0 17.0 39.0 31.0 31.0 23.0 33.0 22.0 ... 20.0 21.0 24.0 34.0 33.0 27.0 29.0 39.0 37.0 5.0
Single Streetlight Issue 189.0 168.0 177.0 215.0 165.0 184.0 221.0 213.0 212.0 212.0 ... 117.0 109.0 154.0 131.0 131.0 138.0 118.0 133.0 128.0 14.0

12 rows × 30 columns


In [ ]: