import csv
with open('just tacos and burritos.csv') as csvfile:
reader = csv.DictReader(csvfile)
test_data = []
for row in reader:
test_data.append(row)
print(len(test_data))
from collections import defaultdict, Counter
toppings_by_state = defaultdict(dict)
toppings_by_state = defaultdict(Counter)
meats = ['beef', 'chicken', 'pork', 'steak', 'fajita', 'tripe', 'asada', 'lingua', 'sesos', 'cabaza', 'fish']
types_of_fish = ['ahui', 'tuna', 'salmon']
for menu_item in test_data:
if 'taco' in (menu_item['menus.name'] + menu_item['menus.description']).lower(): # join item name and description
if len(menu_item['province']) == 2:
state = menu_item['province']
#for description_item in menu_item['menus.description'].split(' '):
# if description_item not in [' ', 'and', '', 'with', 'a', 'red', 'or', 'shredded', 'ground', 'flour', 'of', 'taco', 'on', 'soft',
# 'filled', 'tacos', 'tortillas', 'sour', 'crisp', 'de']: #ignore uninteresting toppings
# state_toppings = toppings_by_state[state].get(description_item, 0)
# state_toppings += 1
# toppings_by_state[state][description_item] = state_toppings
for meat in meats:
added_as_fish = False
if meat in (menu_item['menus.name'] + menu_item['menus.description']).lower():
#state_toppings = toppings_by_state[state].get(meat, 0)
#state_toppings += 1
#toppings_by_state[state][meat] = state_toppings
toppings_by_state[state][meat] += 1
if meat == 'fish':
added_as_fish = True
break
if not added_as_fish:
for fish_type in types_of_fish:
if fish_type in (menu_item['menus.name'] + menu_item['menus.description']).lower():
#state_toppings = toppings_by_state[state].get('fish', 0)
#state_toppings += 1
#toppings_by_state[state]['fish'] = state_toppings
toppings_by_state[state]['fish'] += 1
break
for state in toppings_by_state:
max_state_topping_count = 0
for topping in toppings_by_state[state]:
if toppings_by_state[state][topping] > max_state_topping_count:
max_state_topping_count = toppings_by_state[state][topping]
max_state_topping = topping
print state, max_state_topping, max_state_topping_count
beef_states = {}
fish_states = {}
chicken_states = {}
for state in toppings_by_state:
if len(toppings_by_state[state].most_common(3)) == 3:
first, second, third = toppings_by_state[state].most_common(3)
ratio = first[1] / (first[1] + second[1] + third[1])
if first[0] == 'chicken':
chicken_states[state] = ratio
if first[0] == 'fish':
fish_states[state] = ratio
if first[0] == 'beef':
beef_states[state] = ratio
import pandas as pd
import folium
url = 'https://raw.githubusercontent.com/python-visualization/folium/master/examples/data'
state_geo = '{url}/us-states.json'.format(url=url)
m = folium.Map(location=[48, -102], zoom_start=3)
folium.Choropleth(
geo_data=state_geo,
name='choropleth',
data=beef_states,
columns=['State', 'Beef Taco Ratio'],
key_on='feature.id',
fill_color='YlGn',
fill_opacity=.4,
line_opacity=0.2,
legend_name='Beef Taco Rate (%)'
).add_to(m)
folium.Choropleth(
geo_data=state_geo,
name='choropleth',
data=fish_states,
columns=['State', 'Fish Taco Ratio'],
key_on='feature.id',
fill_color='PuBu',
fill_opacity=.4,
line_opacity=0.2,
legend_name='Fish Taco Rate (%)'
).add_to(m)
folium.Choropleth(
geo_data=state_geo,
name='choropleth',
data=chicken_states,
columns=['State', 'Chicken Taco Ratio'],
key_on='feature.id',
fill_color='Oranges',
fill_opacity=.4,
line_opacity=0.2,
legend_name='Chicken Taco Rate (%)'
).add_to(m)
folium.LayerControl().add_to(m)
m