In [40]:
import os
import charts_function_list
pd.set_option('display.float_format', lambda x: '%.3f' % x)

In [2]:
_,data_folder,outputs = charts_function_list.folder_setup()

In [971]:
os.chdir(data_folder)
health_grades = pd.read_csv('LOS_ANGELES_COUNTY_RESTAURANT_AND_MARKET_INSPECTIONS.csv')

In [972]:
health_grades['ZIP_5']=health_grades['FACILITY ZIP'].apply(lambda x: str(x)[0:5])
health_grades.loc[health_grades['SCORE']<70,'GRADE']='BELOW C'
health_grades = health_grades[health_grades["PROGRAM STATUS"]=='ACTIVE'].copy()
health_grades['FACILITY_WITH_PROGRAM'] = health_grades['FACILITY ID']+health_grades['PROGRAM NAME']

health_grades_active_complete = health_grades

health_grades = health_grades.sort_values(by=['FACILITY_WITH_PROGRAM','ACTIVITY DATE'],ascending=False).drop_duplicates(subset='FACILITY_WITH_PROGRAM').copy()
health_grades.loc[health_grades['PROGRAM NAME'].isnull(),'PROGRAM NAME'] = 'Unknown'

In [969]:
class rest_plots(object):
           
           
    def __init__(self,facecolor,top_pos,group,ymax,rows,columns,width=1,height=2,xmin=79,xmax=102,field='Code Name'):
            self.facecolor = facecolor
            self.top_pos = top_pos
            self.group = group
            self.height = height
            self.width = width
            self.xmin = xmin
            self.xmax = xmax
            self.ymax = ymax
            self.field = field
            self.rows = rows
            self.columns = columns

    def string_search_plots(self,restaurant,left_pos,tag=False,xlabels=False,ylabels=False,multiplier=1,bin_range=33,title=None,new_ticks=False,tick_points=False):  
        ax = plt.subplot2grid((self.rows,self.columns), (self.top_pos,left_pos), colspan=self.width,rowspan=self.height)
        hist =  plt.hist(health_grades[health_grades[self.field]==restaurant]['SCORE'],
                        bins=[(self.xmin+1)+x*multiplier for x in range(bin_range)],edgecolor='white',facecolor=self.facecolor)
        ax.set_ylim(0,self.ymax)
        ax.set_xlim(self.xmin,self.xmax)
        ax.set_xticklabels([int(x) for x in ax.get_xticks()])
        

        #strip labels with if parameter
        if ylabels == True:
            ax.set_ylabel('Count',fontsize=14,color='#525252')
        if xlabels == True:
            ax.set_xlabel('Health Score',fontsize=14,color='#525252',labelpad=0)

        if title != None:
            ax.set_title(title,fontsize=16)
        else:
            ax.set_title(restaurant,fontsize=16)
            
        if tag ==True:
            ax.text(1,.5,self.group,rotation=-90,linespacing=1,fontsize=13,va='center',multialignment='center',transform=ax.transAxes,color=self.facecolor)
        
        if new_ticks== True:
            ax.set_yticks(tick_points)
        return ax

In [869]:
health_grades.loc[health_grades['PROGRAM NAME'].str.contains(str.upper('|'.join(['IN-N-OUT','IN N OUT','IN AND OUT BURGER']))),'Code Name']='In-N-Out'

In [987]:
health_grades['GRADE'].value_counts().loc['A']/health_grades['GRADE'].value_counts().sum()


Out[987]:
0.96866133278801669

In [973]:
#code naming

health_grades.loc[health_grades['FACILITY ADDRESS'].str.contains('WORLD WAY'),'GENERAL CATEGORY']='LAX'
health_grades.loc[health_grades['FACILITY ADDRESS'].str.contains('VIN SCULLY'),'GENERAL CATEGORY']='DODGER STADIUM'
health_grades.loc[health_grades['FACILITY ADDRESS'].isin(['1111 FIGUEROA ST','1111 S FIGUEROA ST']),'GENERAL CATEGORY']='STAPLES CENTER'

health_grades.loc[health_grades['PROGRAM NAME'].str.contains(str.upper('Burger King')),'Code Name']='Burger King'
health_grades.loc[health_grades['PROGRAM NAME'].str.contains(str.upper('|'.join(['McDonalds','McDonald\'s']))),'Code Name']='McDonald\'s'
#health_grades.loc[health_grades['PROGRAM NAME'].str.contains(str.upper('Jack in the Box')),'Code Name']='Jack in the Box'
health_grades.loc[health_grades['PROGRAM NAME'].str.contains(str.upper('|'.join(['IN-N-OUT','IN N OUT','IN AND OUT BURGER']))),'Code Name']='In-N-Out'


health_grades.loc[health_grades['PROGRAM NAME'].str.contains(str.upper('Taco Bell')),'Code Name']='Taco Bell'
health_grades.loc[health_grades['PROGRAM NAME'].str.contains(str.upper('Del Taco')),'Code Name']='Del Taco'
health_grades.loc[health_grades['PROGRAM NAME'].str.contains(str.upper('Chipotle')),'Code Name']='Chipotle'

health_grades.loc[health_grades['PROGRAM NAME'].str.contains('CVS'),'Code Name']='CVS'
health_grades.loc[health_grades['PROGRAM NAME'].str.contains(str.upper('|'.join(['Walgreens','Walgreen\'s']))),'Code Name']='Walgreens'
health_grades.loc[health_grades['PROGRAM NAME'].str.contains(str.upper('|'.join(['Rite-Aid','RiteAid','Rite Aid']))),'Code Name']='Rite-Aid'

health_grades.loc[health_grades['PROGRAM NAME'].str.contains('PIZZA HUT'),'Code Name']='Pizza Hut'

health_grades.loc[health_grades['PROGRAM NAME'].str.contains(str.upper('|'.join(['Papa John','Papa John\'s',
                                                                                 'Papa Johns','Papa Johns\'']))),'Code Name']='Papa John\'s'

health_grades.loc[health_grades['PROGRAM NAME'].str.contains(str.upper('|'.join(['DOMINOS',
                                                                                  'DOMINO\'S','DOMINOS\'']))),'Code Name']='Domino\'s'

In [982]:
fig = plt.figure(figsize=(8.5,11))
fig.suptitle('Some Distributions of Los Angeles County\nRestaurant Health Scores',fontsize=25,color='#d7301f')
plt.subplots_adjust(top=.87)
rows=17
columns=3
main_color='#bdbdbd'


ax = plt.subplot2grid((rows,columns), (0,0), colspan=columns,rowspan=3)
hist =  plt.hist(health_grades['SCORE'],bins=[70+x for x in range(31)],edgecolor='white',facecolor=main_color)
ax.set_title('Overall',fontsize=16)  

ax.set_ylabel('Count',fontsize=14,color='#525252',labelpad=5)
ax.vlines(x=90,ymin=0,ymax=5000,colors='#d7301f')
ax.vlines(x=80,ymin=0,ymax=5000,colors='#d7301f')
ax.vlines(x=70,ymin=0,ymax=5000,colors='#d7301f')
ax.text(70.2,5000,'C Minimum',alpha=.5)
ax.text(80.2,5000,'B Minimum',alpha=.5)
ax.text(90.2,5000,'A Minimum',alpha=.5)

ax.text(70.2,2500,'Note: there are values in the C region,\nbut the scale renders them invisible.\n(See \'C Grades\' chart below)',
        alpha=.5,va='center',fontsize=8)



c_grades = rest_plots(top_pos=4,group='C',ymax=12,xmin=69,height=1,xmax=81,facecolor='#525252',field='GRADE',rows=rows,columns=columns)
c_grades.string_search_plots('C',left_pos=0,ylabels=True,multiplier=1,bin_range=11,title='C Grades')

b_grades = rest_plots(top_pos=4,group='B',ymax=160,xmin=79,height=1,xmax=90,facecolor='#525252',field='GRADE',rows=rows,columns=columns)
b_grades.string_search_plots('B',left_pos=1,multiplier=1,bin_range=11,title='B Grades')

a_grades = rest_plots(top_pos=4,group='C',ymax=5000,xmin=89,height=1,xmax=101,facecolor='#525252',field='GRADE',rows=rows,columns=columns)
ax=a_grades.string_search_plots('A',left_pos=2,multiplier=1,bin_range=11,title='A Grades')
ax.set_yticks([0,2500,5000])

tacos = rest_plots(facecolor='#feb24c',top_pos=9,group='"Mexican"\nFast Food',ymax=30,rows=rows,columns=columns)
tacos.string_search_plots('Del Taco',left_pos=0,ylabels=True,new_ticks=True,tick_points=[10,20,30])
tacos.string_search_plots('Chipotle',left_pos=1,new_ticks=True,tick_points=[10,20,30])
tacos.string_search_plots('Taco Bell',left_pos=2,tag=True,new_ticks=True,tick_points=[10,20,30])


burgers = rest_plots(facecolor='#fc9272',top_pos=6,group='Burger\nFast Food',ymax=35,rows=rows,columns=columns)
burgers.string_search_plots('In-N-Out',left_pos=0,ylabels=True,new_ticks=True,tick_points=[10,20,30])
burgers.string_search_plots('Burger King',left_pos=1,new_ticks=True,tick_points=[10,20,30])
burgers.string_search_plots('McDonald\'s',left_pos=2,tag=True,new_ticks=True,tick_points=[10,20,30])

#pizza = rest_plots(facecolor='#b30000',top_pos=13,group='Pizza\nFast Food',ymax=35,rows=rows,columns=columns)
#pizza.string_search_plots('Papa John\'s',left_pos=0,ylabels=True)
#pizza.string_search_plots('Domino\'s',left_pos=1)
#pizza.string_search_plots('Pizza Hut',left_pos=2,tag=True)

drugs = rest_plots(facecolor='#66c2a4',top_pos=12,group='Drug Stores',ymax=43,rows=rows,columns=columns)
drugs.string_search_plots('Walgreens',left_pos=0,ylabels=True,new_ticks=True,tick_points=[10,20,30,40])
drugs.string_search_plots('Rite-Aid',left_pos=1,new_ticks=True,tick_points=[10,20,30,40])
drugs.string_search_plots('CVS',left_pos=2,tag=True,new_ticks=True,tick_points=[10,20,30,40])

multi_place = rest_plots(facecolor='#225ea8',top_pos=15,group='Many-in-one',ymax=20,rows=rows,columns=columns,field='GENERAL CATEGORY')

ax=multi_place.string_search_plots('STAPLES CENTER',left_pos=0,title='Staples Center',ylabels=True,xlabels=True)
multi_place.string_search_plots('DODGER STADIUM',left_pos=1,title='Dodger Stadium',xlabels=True)
multi_place.string_search_plots('LAX',left_pos=2,tag=True,xlabels=True)

ax.text(0,-.85,'Source: data.lacounty.gov (data as of 3/22/2018)',transform=ax.transAxes,alpha=.5)
ax.text(0,-1.05,'IG/Twitter: @igotcharts|www.igotcharts.com, 2018',transform=ax.transAxes,alpha=.5)
os.chdir(outputs)
charts_function_list.chart_save('LA_county_health',dpi=150)


Other ideas to explore in the future


In [95]:
def pivot_combine(index_type):
    grade_summary = health_grades.pivot_table('SCORE',index=index_type,columns='GRADE',aggfunc='count',margins=True).fillna(0)
    grade_summary_per = grade_summary[['A','B','BELOW C','C']].div(grade_summary['All'],axis=0)
    grade_summary_per['B_Or_Below_Per']=grade_summary_per[['B','BELOW C','C']].sum(axis=1)

    grade_summary_per = grade_summary_per.rename(columns={'A':'A Percent',
                                             'B':'B Percent',
                                            'C':'C Percent',
                                            'BELOW C':'BELOW C Percent'})

    return pd.merge(grade_summary,grade_summary_per,left_index=True,right_index=True,how='left')

In [107]:
pivot_combine('ZIP_5').sort_values(by='C Percent',ascending=False)


Out[107]:
GRADE A B BELOW C C All A Percent B Percent BELOW C Percent C Percent B_Or_Below_Per
ZIP_5
91749 0.000 2.000 0.000 2.000 4.000 0.000 0.500 0.000 0.500 1.000
90510 4.000 0.000 0.000 1.000 5.000 0.800 0.000 0.000 0.200 0.200
90755 214.000 26.000 1.000 11.000 252.000 0.849 0.103 0.004 0.044 0.151
90506 24.000 0.000 0.000 1.000 25.000 0.960 0.000 0.000 0.040 0.040
90810 60.000 16.000 0.000 3.000 79.000 0.759 0.203 0.000 0.038 0.241
91755 186.000 40.000 1.000 8.000 235.000 0.791 0.170 0.004 0.034 0.209
90704 171.000 59.000 0.000 7.000 237.000 0.722 0.249 0.000 0.030 0.278
93243 38.000 2.000 0.000 1.000 41.000 0.927 0.049 0.000 0.024 0.073
91754 936.000 269.000 2.000 26.000 1233.000 0.759 0.218 0.002 0.021 0.241
90020 1240.000 214.000 0.000 27.000 1481.000 0.837 0.144 0.000 0.018 0.163
90651 53.000 2.000 0.000 1.000 56.000 0.946 0.036 0.000 0.018 0.054
91108 119.000 10.000 0.000 2.000 131.000 0.908 0.076 0.000 0.015 0.092
90005 1129.000 194.000 2.000 18.000 1343.000 0.841 0.144 0.001 0.013 0.159
90057 717.000 98.000 0.000 11.000 826.000 0.868 0.119 0.000 0.013 0.132
90044 560.000 49.000 1.000 8.000 618.000 0.906 0.079 0.002 0.013 0.094
90010 825.000 77.000 0.000 11.000 913.000 0.904 0.084 0.000 0.012 0.096
93591 79.000 4.000 0.000 1.000 84.000 0.940 0.048 0.000 0.012 0.060
90746 463.000 39.000 3.000 6.000 511.000 0.906 0.076 0.006 0.012 0.094
91304 565.000 34.000 0.000 7.000 606.000 0.932 0.056 0.000 0.012 0.068
90061 253.000 30.000 1.000 3.000 287.000 0.882 0.105 0.003 0.010 0.118
91001 271.000 17.000 0.000 3.000 291.000 0.931 0.058 0.000 0.010 0.069
93550 761.000 33.000 0.000 8.000 802.000 0.949 0.041 0.000 0.010 0.051
90249 653.000 69.000 2.000 7.000 731.000 0.893 0.094 0.003 0.010 0.107
90745 862.000 69.000 2.000 9.000 942.000 0.915 0.073 0.002 0.010 0.085
90002 203.000 6.000 0.000 2.000 211.000 0.962 0.028 0.000 0.009 0.038
91401 743.000 20.000 0.000 7.000 770.000 0.965 0.026 0.000 0.009 0.035
90017 948.000 53.000 1.000 9.000 1011.000 0.938 0.052 0.001 0.009 0.062
91803 390.000 61.000 0.000 4.000 455.000 0.857 0.134 0.000 0.009 0.143
90003 816.000 86.000 0.000 8.000 910.000 0.897 0.095 0.000 0.009 0.103
93543 111.000 2.000 0.000 1.000 114.000 0.974 0.018 0.000 0.009 0.026
90058 327.000 23.000 0.000 3.000 353.000 0.926 0.065 0.000 0.008 0.074
90255 1373.000 120.000 0.000 12.000 1505.000 0.912 0.080 0.000 0.008 0.088
90066 706.000 42.000 1.000 6.000 755.000 0.935 0.056 0.001 0.008 0.065
91775 125.000 2.000 0.000 1.000 128.000 0.977 0.016 0.000 0.008 0.023
91306 485.000 23.000 0.000 4.000 512.000 0.947 0.045 0.000 0.008 0.053
90019 1055.000 95.000 1.000 9.000 1160.000 0.909 0.082 0.001 0.008 0.091
90270 490.000 23.000 0.000 4.000 517.000 0.948 0.044 0.000 0.008 0.052
91607 504.000 11.000 0.000 4.000 519.000 0.971 0.021 0.000 0.008 0.029
90254 588.000 63.000 0.000 5.000 656.000 0.896 0.096 0.000 0.008 0.104
91605 986.000 62.000 0.000 8.000 1056.000 0.934 0.059 0.000 0.008 0.066
90064 1121.000 67.000 0.000 9.000 1197.000 0.937 0.056 0.000 0.008 0.063
90062 258.000 7.000 0.000 2.000 267.000 0.966 0.026 0.000 0.007 0.034
90006 1573.000 166.000 0.000 13.000 1752.000 0.898 0.095 0.000 0.007 0.102
90304 520.000 38.000 0.000 4.000 562.000 0.925 0.068 0.000 0.007 0.075
90065 542.000 39.000 0.000 4.000 585.000 0.926 0.067 0.000 0.007 0.074
91342 703.000 31.000 0.000 5.000 739.000 0.951 0.042 0.000 0.007 0.049
90602 430.000 13.000 1.000 3.000 447.000 0.962 0.029 0.002 0.007 0.038
90404 413.000 31.000 0.000 3.000 447.000 0.924 0.069 0.000 0.007 0.076
91801 897.000 140.000 1.000 7.000 1045.000 0.858 0.134 0.001 0.007 0.142
90025 1412.000 85.000 1.000 10.000 1508.000 0.936 0.056 0.001 0.007 0.064
... ... ... ... ... ... ... ... ... ... ...
91325 479.000 10.000 0.000 0.000 489.000 0.980 0.020 0.000 0.000 0.020
91326 245.000 6.000 0.000 0.000 251.000 0.976 0.024 0.000 0.000 0.024
91328 3.000 0.000 0.000 0.000 3.000 1.000 0.000 0.000 0.000 0.000
91330 124.000 0.000 0.000 0.000 124.000 1.000 0.000 0.000 0.000 0.000
91333 7.000 0.000 0.000 0.000 7.000 1.000 0.000 0.000 0.000 0.000
90274 329.000 5.000 0.000 0.000 334.000 0.985 0.015 0.000 0.000 0.015
91341 5.000 0.000 0.000 0.000 5.000 1.000 0.000 0.000 0.000 0.000
90070 4.000 0.000 0.000 0.000 4.000 1.000 0.000 0.000 0.000 0.000
91343 550.000 45.000 0.000 0.000 595.000 0.924 0.076 0.000 0.000 0.076
90069 1176.000 60.000 0.000 0.000 1236.000 0.951 0.049 0.000 0.000 0.049
91346 13.000 0.000 0.000 0.000 13.000 1.000 0.000 0.000 0.000 0.000
91351 691.000 5.000 0.000 0.000 696.000 0.993 0.007 0.000 0.000 0.007
90068 262.000 10.000 0.000 0.000 272.000 0.963 0.037 0.000 0.000 0.037
91354 90.000 3.000 0.000 0.000 93.000 0.968 0.032 0.000 0.000 0.032
91355 1540.000 9.000 0.000 0.000 1549.000 0.994 0.006 0.000 0.000 0.006
91357 4.000 0.000 0.000 0.000 4.000 1.000 0.000 0.000 0.000 0.000
91359 1.000 0.000 0.000 0.000 1.000 1.000 0.000 0.000 0.000 0.000
91361 103.000 2.000 0.000 0.000 105.000 0.981 0.019 0.000 0.000 0.019
91364 976.000 5.000 0.000 0.000 981.000 0.995 0.005 0.000 0.000 0.005
91367 816.000 10.000 0.000 0.000 826.000 0.988 0.012 0.000 0.000 0.012
90086 5.000 0.000 0.000 0.000 5.000 1.000 0.000 0.000 0.000 0.000
91302 482.000 2.000 0.000 0.000 484.000 0.996 0.004 0.000 0.000 0.004
91016 1011.000 21.000 0.000 0.000 1032.000 0.980 0.020 0.000 0.000 0.020
91301 534.000 2.000 0.000 0.000 536.000 0.996 0.004 0.000 0.000 0.004
91018 3.000 0.000 0.000 0.000 3.000 1.000 0.000 0.000 0.000 0.000
91020 330.000 4.000 0.000 0.000 334.000 0.988 0.012 0.000 0.000 0.012
91023 11.000 0.000 0.000 0.000 11.000 1.000 0.000 0.000 0.000 0.000
91024 141.000 2.000 0.000 0.000 143.000 0.986 0.014 0.000 0.000 0.014
91028 8.000 0.000 0.000 0.000 8.000 1.000 0.000 0.000 0.000 0.000
91029 6.000 1.000 0.000 0.000 7.000 0.857 0.143 0.000 0.000 0.143
91031 3.000 0.000 0.000 0.000 3.000 1.000 0.000 0.000 0.000 0.000
91040 316.000 12.000 0.000 0.000 328.000 0.963 0.037 0.000 0.000 0.037
90095 24.000 1.000 0.000 0.000 25.000 0.960 0.040 0.000 0.000 0.040
91104 42.000 3.000 0.000 0.000 45.000 0.933 0.067 0.000 0.000 0.067
90094 107.000 8.000 0.000 0.000 115.000 0.930 0.070 0.000 0.000 0.070
90089 212.000 0.000 0.000 0.000 212.000 1.000 0.000 0.000 0.000 0.000
91109 24.000 0.000 0.000 0.000 24.000 1.000 0.000 0.000 0.000 0.000
91122 4.000 0.000 0.000 0.000 4.000 1.000 0.000 0.000 0.000 0.000
91201 590.000 4.000 0.000 0.000 594.000 0.993 0.007 0.000 0.000 0.007
91202 347.000 7.000 0.000 0.000 354.000 0.980 0.020 0.000 0.000 0.020
91203 605.000 14.000 0.000 0.000 619.000 0.977 0.023 0.000 0.000 0.023
91204 659.000 17.000 0.000 0.000 676.000 0.975 0.025 0.000 0.000 0.025
91206 462.000 16.000 0.000 0.000 478.000 0.967 0.033 0.000 0.000 0.033
91207 35.000 0.000 0.000 0.000 35.000 1.000 0.000 0.000 0.000 0.000
91208 176.000 5.000 0.000 0.000 181.000 0.972 0.028 0.000 0.000 0.028
91209 14.000 2.000 0.000 0.000 16.000 0.875 0.125 0.000 0.000 0.125
91210 352.000 5.000 0.000 0.000 357.000 0.986 0.014 0.000 0.000 0.014
91212 3.000 0.000 0.000 0.000 3.000 1.000 0.000 0.000 0.000 0.000
91214 390.000 17.000 0.000 0.000 407.000 0.958 0.042 0.000 0.000 0.042
90808 5.000 0.000 0.000 0.000 5.000 1.000 0.000 0.000 0.000 0.000

381 rows × 10 columns