Why is my choropleth map in python empty? - python

So I am trying to learn to plot choropleth maps. I used a sample dataset but it comes out empty. Please could you look at it and tell me what is wrong because I have crosschecked the syntax and do not understand why it does not show my data. I have included the dataset head and the code I wrote as seen below:
datia = dict(type = 'choropleth',
locations = df['Names'],
locationmode = 'USA-states',
colorscale= 'Portland',
text= df['Names'],
z=df['2010-11'],
colorbar = {'title':'Vaccine Coverage in percentage'})
layout = dict(title = 'Vaccine Coverage in percentage',
geo = {'scope':'usa'})
choromap = go.Figure(data = [datia],layout = layout)
iplot(choromap, validate=False)

You need to give STATE Abbreviations(DC,AL,...) instead of Names.
I faced the same issue. It worked after changing that.
You can use the following code to convert names to abbreviations
def convert_state_name(name):
cd=str(name)
us_state_abbrev = {
"Alabama":"AL"
"Alaska":"AK"
"Arizona":"AZ"
"Arkansas":"AR"
"California":"CA"
"Colorado":"CO"
"Connecticut":"CT"
"Delaware":"DE"
"Washington DC":"DC"
"Florida":"FL"
"Georgia":"GA"
"Hawaii":"HI"
"Idaho":"ID"
"Illinois":"IL"
"Indiana":"IN"
"Iowa":"IA"
"Kansas":"KS"
"Kentucky":"KY"
"Louisiana":"LA"
"Maine":"ME"
"Maryland":"MD"
"Massachusetts":"MA"
"Michigan":"MI"
"Minnesota":"MN"
"Mississippi":"MS"
"Missouri":"MO"
"Montana":"MT"
"Nebraska":"NE"
"Nevada":"NV"
"New Hampshire":"NH"
"New Jersey":"NJ"
"New Mexico":"NM"
"New York":"NY"
"North Carolina":"NC"
"North Dakota":"ND"
"Ohio":"OH"
"Oklahoma":"OK"
"Oregon":"OR"
"Pennsylvania":"PA"
"Rhode Island":"RI"
"South Carolina":"SC"
"South Dakota":"SD"
"Tennessee":"TN"
"Texas":"TX"
"Utah":"UT"
"Vermont":"VT"
"Virginia":"VA"
"Washington":"WA"
"West Virginia":"WV"
"Wisconsin":"WI"
"Wyoming":"WY"
return us_state_abbrev[cd]
df['STATE_CD'] = df.Names.apply(lambda x:convert_state_name(x))
Finally you can replace locations=df['Names'] with locations=df['STATE_CD']
Hope this helps.

Related

Python JSON assign data from API

I have a py file to read data from Wordpress API and pass values to another fields of other API. When values are singles, i have no problem, but i don't know how make that:
When i read one field from the API, the states values, comes with code instead the text value. For example, when the text value in Wordpress is Barcelona, returns B, and i'll need that the value returned will be Barcelona.
One example of code with simple fields values:
oClienteT["Direcciones"] = []
oClienteT["Telefono"] = oClienteW["billing"]["phone"]
oClienteT["NombreFiscal"] = oClienteW["first_name"] " " oClienteW["last_name"]
oClienteT["Direcciones"].append( {
"Codigo" : oClienteW["id"],
"Nombre" : oClienteW["billing"]["first_name"],
"Apellidos" : oClienteW["billing"]["last_name"],
"Direccion" : oClienteW["billing"]["address_1"],
"Direccion2" : oClienteW["billing"]["address_2"],
"Poblacion" : oClienteW["billing"]["state"],
"Provincia" : oClienteW["billing"]["city"]
})
When billing city is Madrid and billing state is madrid, Wordpress returns Madrid and M
I need tell thst when Madrid, returns Madrid, and so on.
Make sure to convert to a JSON object before accessing fields (data = json.loads(json_str))
response = { "billing": { "address_1": "C/GUSTAVO ADOLFO BECQUER, 4", "city": "SEVILLA", "state": "SE"}}
print(response["billing"].get("address_1", None))
I've just got solved it:
def initProvincias(self):
self.aProvincias['C'] = 'A Coruña'
self.aProvincias['VI'] = 'Álava'
def getProvincia(self , sCod ):
if not sCod in self.aProvincias:
_logger.info("PROVINCIA NO ENCONTRADA "+str(sCod))
return ""
return self.aProvincias[sCod]
"Provincia" : self.getProvincia( oClienteW["shipping"]["state"] ),

Is there a way to control which vertices connect in a plotly.express.line_geo map?

I'm trying to make a connection map that has the option to use an animation_frame to show different months/years. Plotly.express has this option, but the plotly.express.line_geo maps seem to just attach the vertices of the network at random. I was looking at these examples from https://plotly.com/python/lines-on-maps/.
import plotly.express as px
df = px.data.gapminder().query("year == 2007")
fig = px.line_geo(df, locations="iso_alpha",
color="continent", # "continent" is one of the columns of gapminder
projection="orthographic")
fig.show()
Plotly.graph_objects allows you to map actual connections between vertices, but doesn't seem to have an animation option.
import plotly.graph_objects as go
import pandas as pd
df_airports = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_us_airport_traffic.csv')
df_airports.head()
df_flight_paths = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_aa_flight_paths.csv')
df_flight_paths.head()
fig = go.Figure()
flight_paths = []
for i in range(len(df_flight_paths)):
fig.add_trace(
go.Scattergeo(
locationmode = 'USA-states',
lon = [df_flight_paths['start_lon'][i], df_flight_paths['end_lon'][i]],
lat = [df_flight_paths['start_lat'][i], df_flight_paths['end_lat'][i]],
mode = 'lines',
line = dict(width = 1,color = 'red'),
opacity = float(df_flight_paths['cnt'][i]) / float(df_flight_paths['cnt'].max()),
)
)
fig.show()
Does anyone know of a way that i could make a map like the flight path map, but allow an animation option to look at the flight maps for different months/years?
you can animate any trace type using frames
taking sample flight path data used in question, have split it into groups based on first letter of start airport
there is no need to create a trace per flight, instead create pairs of start end locations in arrays separated by None
with this it is simple to create a frame with a trace for each group
then just create the figure from the frames, plus a default trace
add play button and slider
import plotly.graph_objects as go
import plotly.express as px
import pandas as pd
df_flight_paths = pd.read_csv(
"https://raw.githubusercontent.com/plotly/datasets/master/2011_february_aa_flight_paths.csv"
)
frames = []
# lets split data based on first letter of start airport
# create a frame for each grouping
bins = 6
for color, df in df_flight_paths.groupby(
pd.qcut(
df_flight_paths["airport1"].str[0].apply(ord),
q=bins,
labels=px.colors.qualitative.Plotly[:bins],
)
):
name = f'{df["airport1"].str[0].min()}-{df["airport1"].str[0].max()}'
frames.append(
go.Frame(
name=name,
data=go.Scattergeo(
lon=df.assign(nan=None)[["start_lon", "end_lon", "nan"]].values.ravel(),
lat=df.assign(nan=None)[["start_lat", "end_lat", "nan"]].values.ravel(),
mode="lines",
line=dict(width=1, color=color),
),
)
)
# now create figure and add play button and slider
go.Figure(
data=frames[0].data,
frames=frames,
layout={
"updatemenus": [
{
"type": "buttons",
"buttons": [{"label": "Play", "method": "animate", "args": [None]}],
}
],
"sliders": [
{
"active": 0,
"steps": [
{
"label": f.name,
"method": "animate",
"args": [[f.name]],
}
for f in frames
],
}
],
},
).update_geos(
scope="north america",
)

PYTHON: How to get seaborn attributes to reset to default?

I cannot for the life of me get seaborn to go back to default settings.
I will place the code that I believe caused this issue, but I would recommend against running it unless you know how to fix it.
The culprit I believe is in the last chunk
sns.set(font_scale = 4)
before this question gets deleted because it's already been asked, I have tried the other posted solutions with no success. Just to name a quick few, resetting using sns.set(), sns.set_style(), sns.restore_defaults(). I have also tried resetting matplot settings to defaults as well. This attribute persists across all my files, so I cant even open a new file, delete the line of code that caused it, or run any past programs, or it will apply to those graphs too.
My seaborn version is 0.10.1, I have tried to update it, but I cant get it to go through. I am using anaconda's spyder IDE
The documentation says for versions after 0.8 that the styles/themes must be invoked to be reset, but if I try to use their solution sns.set_theme() I get an error saying that this module has no attribute.
I'm sure this persistence is considered a feature, but I desperately need it to go away!
import requests
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import os
if __name__ == '__main__':
#data prep
data_path = './assets/'
out_path = './output'
#scraping javascript map data via xml
endpoint = "https://covid.cdc.gov/covid-data-tracker/COVIDData/getAjaxData"
data = requests.get(endpoint, params={"id": "US_MAP_DATA"}).json()
#convert to df and export raw data as csv
df = pd.DataFrame(data["US_MAP_DATA"])
path = os.path.join(out_path,'Raw_CDC_Data.csv')
df.to_csv(path)
#Remove last data point (Total USA)
df.drop(df.tail(1).index,inplace=True)
#Create DF of just 50 states
state_abbr =["AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DC", "DE", "FL", "GA",
"HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD",
"MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ",
"NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC",
"SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"]
states = df[df['abbr'].isin(state_abbr)]
#Adding NYC to state of NY
# FILL THIS IN LATER
#Graphing
plt.style.use('default')
sns.set()
#add new col survival rate and save
states['survival_rate']=states['tot_cases']-states['tot_death']
states.drop(df.columns[[0]],axis=1)
states.reset_index(drop=True)
path = os.path.join(out_path,'CDC_Data_By_State.csv')
states.to_csv(path)
#Stacked BarPlot
fig, ax = plt.subplots()
colors = ['#e5c5b5','#a8dda8']
r=range(0,len(states.index))
plt.bar(r,states['survival_rate'],color=colors[0])
#ax = stacked['survival_rate','tot_death'].plot.bar(stacked=True, color=colors, ax=ax)
fig, ax = plt.subplots()
plt.figure(figsize=(20,35))
sns.set(font_scale=4)
ax = sns.barplot(x='tot_cases',y='abbr',data=states)
ax.set(title='USA Covid-19 Cases by State', ylabel='State', xlabel='Confirmed Cases')
path = os.path.join(out_path,'Total_Deaths_Bar.png')
plt.savefig(path)

Get hyperlink from a cell in google Sheet api V4

I want to get the hyperlink of a cell (A1, for example) in Python. I have this code so far. Thanks
properties = {
"requests": [
{
"cell": {
"HyperlinkDisplayType": "LINKED"
},
"fields": "userEnteredFormat.HyperlinkDisplayType"
}
]
}
result = service.spreadsheets().values().get(
spreadsheetId=spreadsheet_id, range=rangeName, body=properties).execute()
values = result.get('values', [])
How about using sheets.spreadsheets.get? This sample script supposes that service of your script has already been able to be used for spreadsheets().values().get().
Sample script :
spreadsheetId = '### Spreadsheet ID ###'
range = ['sheet1!A1:A1'] # This is a sample.
result = service.spreadsheets().get(
spreadsheetId=spreadsheetId,
ranges=range,
fields="sheets/data/rowData/values/hyperlink"
).execute()
If this was not useful for you, I'm sorry.
It seems to me like this is the only way to actually get the link info (address as well as display text):
result = service.spreadsheets().values().get(
spreadsheetId=spreadsheetId, range=range_name,
valueRenderOption='FORMULA').execute()
values = results.get('values', [])
This returns the raw content of the cells which for hyperlinks look like this for each cell:
'=HYPERLINK("sample-link","http://www.sample.com")'
For my use I've parsed it with the following simple regex:
r'=HYPERLINK\("(.*?)","(.*?)"\)'
You can check the hyperlink if you add at the end:
print (values[0])

Retrieve the country from the geographical locations in Python

I am trying to get the country name from the latitude and longitude points from my pandas dataframe.
Currently I have used geolocator.reverse(latitude,longitude) to get the full address of the geographic location. But there is no option to retrieve the country name from the full address as it returns a list.
Method used:
def get_country(row):
pos = str(row['StartLat']) + ', ' + str(row['StartLong'])
locations = geolocator.reverse(pos)
return locations
Call to get_country by passing the dataframe:
df4['country'] = df4.apply(lambda row: get_country(row), axis = 1)
Current output:
StartLat StartLong Address
52.509669 13.376294 Potsdamer Platz, Mitte, Berlin, Deutschland, Europe
Just wondering whether there is some Python library to retrieve the country when we pass the geographic points.
Any help would be appreciated.
In your get_country function, your return value location will have an attribute raw, which is a dict that looks like this:
{
'address': {
'attraction': 'Potsdamer Platz',
'city': 'Berlin',
'city_district': 'Mitte',
'country': 'Deutschland',
'country_code': 'de',
'postcode': '10117',
'road': 'Potsdamer Platz',
'state': 'Berlin'
},
'boundingbox': ['52.5093982', '52.5095982', '13.3764983', '13.3766983'],
'display_name': 'Potsdamer Platz, Mitte, Berlin, 10117, Deutschland',
... and so one ...
}
so location.raw['address']['country'] gives 'Deutschland'
If I read your question correctly, a possible solution could be:
def get_country(row):
pos = str(row['StartLat']) + ', ' + str(row['StartLong'])
locations = geolocator.reverse(pos)
return location.raw['address']['country']
EDIT: The format of the location.raw object will differ depending on which geolocator service you are using. My example uses geopy.geocoders.Nominatim, from the example on geopy's documentation site, so your results might differ.
My code,hopefully that helps:
from geopy.geocoders import Nominatim
nm = Nominatim()
place, (lat, lng) = nm.geocode("3995 23rd st, San Francisco,CA 94114")
print('Country' + ": " + place.split()[-1])
I'm not sure what service you're using with geopy, but as a small plug which I'm probably biased towards, this I think could be a simpler solution for you.
https://github.com/Ziptastic/ziptastic-python
from ziptastic import Ziptastic
# Set API key.
api = Ziptastic('<your api key>')
result = api.get_from_coordinates('42.9934', '-84.1595')
Which will return a list of dictionaries like so:
[
{
"city": "Owosso",
"geohash": "dpshsfsytw8k",
"country": "US",
"county": "Shiawassee",
"state": "Michigan",
"state_short": "MI",
"postal_code": "48867",
"latitude": 42.9934,
"longitude": -84.1595,
"timezone": "America/Detroit"
}
]

Categories

Resources