# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python [conda env:ada]
#     language: python
#     name: conda-env-ada-py
# ---

# +
import numpy as np 
import requests

def get_symbol(symbol):
    url = "http://d.yimg.com/autoc.finance.yahoo.com/autoc?query={}&region=1&lang=en".format(symbol)

    result = requests.get(url).json()

    for x in result['ResultSet']['Result']:
        if x['symbol'] == symbol:
            return x['name']


# -

company = get_symbol("AAPL")
print(company)

import wikipedia
print (wikipedia.summary(company))

# +
#Usually the first sentence of wikipedia states the localisation of the company. we tried to get this information differently (looking in the stock directly but it was not possible ... )
# -

wikisummary = wikipedia.WikipediaPage(company).summary

# +
US_states = ["Alabama","Alaska","Arizona","Arkansas","California","Colorado",
  "Connecticut","Delaware","Florida","Georgia","Hawaii","Idaho","Illinois",
  "Indiana","Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland",
  "Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana",
  "Nebraska","Nevada","New Hampshire","New Jersey","New Mexico","New York",
  "North Carolina","North Dakota","Ohio","Oklahoma","Oregon","Pennsylvania",
  "Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah",
  "Vermont","Virginia","Washington","West Virginia","Wisconsin","Wyoming"]
 
apparences = []
for i in US_states:
    apparences.append(wikisummary.find(i))
    
print(company ,' state is ', US_states[np.argmax(apparences)])
# -

import sys
import pprint
from finsymbols import symbols
sp500 = symbols.get_sp500_symbols()

# +
#amex = symbols.get_amex_symbols()
#nyse = symbols.get_nyse_symbols()
#nasdaq = symbols.get_nasdaq_symbols()
# -

#all_stocks = sp500 + amex + nyse + nasdaq
all_stocks = sp500
len(all_stocks)

# +
import pandas as pd

symbols = []
company = []
sector = []
industry = []
headquarters = []

for item in all_stocks: 
    symbols.append(item["symbol"] )
    company.append (item['company'])
    sector.append(item['sector'])
    industry.append(item['industry'])
    headquarters.append(item['headquarters'])
    
data = {'symbol' : symbols, 'company' : company, 'sector':sector, 'industry': industry, 'headquarters':headquarters}
df = pd.DataFrame(data)

# -

df_detailled = pd.DataFrame(df.headquarters.str.split(', ',1).tolist(), columns = ['city','state'])
df_detailled = df.assign(city = df_detailled.city, state = df_detailled.state)
df_detailled.drop(columns = 'headquarters', inplace = True )
df_detailled

us_state_abbrev = {
    'AL': 'Alabama',
    'AK': 'Alaska',
    'AZ': 'Arizona',
    'AR': 'Arkansas',
    'CA': 'California',
    'CO': 'Colorado',
    'CT': 'Connecticut',
    'DE': 'Delaware',
    'FL': 'Florida',
    'GA': 'Georgia',
    'HI': 'Hawaii',
    'ID': 'Idaho',
    'IL': 'Illinois',
    'IN': 'Indiana',
    'IA': 'Iowa',
    'KS': 'Kansas',
    'KY': 'Kentucky',
    'LA': 'Louisiana',
    'ME': 'Maine',
    'MD': 'Maryland',
    'MA': 'Massachusetts',
    'MI': 'Michigan',
    'MN': 'Minnesota',
    'MS': 'Mississippi',
    'MO': 'Missouri',
    'MT': 'Montana',
    'NE': 'Nebraska',
    'NV': 'Nevada',
    'NH': 'New Hampshire',
    'NJ': 'New Jersey',
    'NM': 'New Mexico',
    'NY': 'New York',
    'NC': 'North Carolina',
    'ND': 'North Dakota',
    'OH': 'Ohio',
    'OK': 'Oklahoma',
    'OR': 'Oregon',
    'PA': 'Pennsylvania',
    'RI': 'Rhode Island',
    'SC': 'South Carolina',
    'SD': 'South Dakota',
    'TN': 'Tennessee',
    'TX': 'Texas',
    'UT': 'Utah',
    'VT': 'Vermont',
    'VA': 'Virginia',
    'WA': 'Washington',
    'WV': 'West Virginia',
    'WI': 'Wisconsin',
    'WY': 'Wyoming',
    'DC': 'D.C.', 
    'MP':'Northern Mariana Islands', 
    'PW' : 'Palau',  
    'PR':'Puerto Rico', 
    'VI': 'Virgin Islands',
}


def convert_to_code(df_detailled):
    code = []
    noisy_data = []

    for i in df_detailled['state']: 
        found = False 
        for key, value in us_state_abbrev.items():
            if i.lower().strip() == value.lower().strip(): 
                code.append(str(key)) 
                found = True

        if not found: 
            noisy_data.append(i)

    #remove the noisy data 
    for i in noisy_data: 
        df_detailled = df_detailled[df_detailled['state'] != i]

    return df_detailled.assign(code = code)


df_detailled = convert_to_code(df_detailled)
df_detailled.to_csv('localization_of_companies.csv')

# +
# add the scores of the companies computed in the other parts of the project 
# -

df_score = pd.read_csv('../data/company_scores.csv')

df_score

df_merged = pd.merge(df_score, df_detailled, on='symbol')
df_merged

df_mean = df_merged.groupby('state').mean()
df_mean = df_mean.reset_index()
df_mean = convert_to_code(df_mean)
df_mean

# +
import plotly.plotly as py
import pandas as pd

df = df_mean

# +
for col in df.columns:
    df[col] = df[col].astype(str)

scl = [[0.0, 'rgb(242,240,247)'],[0.2, 'rgb(218,218,235)'],[0.4, 'rgb(188,189,220)'],\
            [0.6, 'rgb(158,154,200)'],[0.8, 'rgb(117,107,177)'],[1.0, 'rgb(84,39,143)']]


df['text'] = df['state'] + '<br>' + 'Price '+df['PRICE']+ '<br>' + ' Rank Overall ' + df['rank.overall']+ '<br>'+\
    'pcent'+ df['pcent'] + '<br>' +' USD ' + df['usd']

data = [ dict(
        type='choropleth',
        colorscale = scl,
        autocolorscale = False,
        locations = df['code'],
        z = df['weighted_score'].astype(float),
        locationmode = 'USA-states',
        text = df['text'],
        marker = dict(
            line = dict (
                color = 'rgb(255,255,255)',
                width = 2
            ) ),
        colorbar = dict(
            title = "[-]")
        ) ]

layout = dict(
        title = '2017 Social Investment Score by State',
        geo = dict(
            scope='usa',
            projection=dict( type='albers usa' ),
            showlakes = True,
            lakecolor = 'rgb(255, 255, 255)'),
             )
    
fig = dict( data=data, layout=layout )
py.iplot( fig, filename='d3-cloropleth-map' )
# -

import plotly
plotly.offline.plot(fig, filename='geographical_weighted_scores.html', auto_open=False)



StarcoderdataJupyterScriptsDedupFiltered