In Python, I'm plotting a choropleth with some data for some countries in Africa:
countries = ['BDI', 'BEN', 'BFA', 'BWA', 'CIV', 'CMR', 'COD', 'CPV', 'ETH', 'GHA', 'GIN', 'GMB', 'KEN', 'LBR', 'LSO', 'MDG', 'MLI', 'MOZ', 'MUS', 'MWI', 'NER', 'NGA', 'RWA', 'SEN', 'SLE', 'SOM', 'STP', 'TCD', 'TGO', 'TZA', 'UGA', 'ZAF', 'ZMB', 'ZWE']
z = [5, 6, 1, 1, 2, 14, 7, 1, 3, 6, 1, 2, 13, 1, 3, 11, 4, 2, 1, 6, 1, 50, 18, 5, 2, 4, 1, 1, 4, 16, 15, 4, 10, 4]
Plotting this data like so:
import plotly.offline as py
import plotly.graph_objs as go
countries = ['BDI', 'BEN', 'BFA', 'BWA', 'CIV', 'CMR', 'COD', 'CPV', 'ETH', 'GHA', 'GIN', 'GMB', 'KEN', 'LBR', 'LSO', 'MDG', 'MLI', 'MOZ', 'MUS', 'MWI', 'NER', 'NGA', 'RWA', 'SEN', 'SLE', 'SOM', 'STP', 'TCD', 'TGO', 'TZA', 'UGA', 'ZAF', 'ZMB', 'ZWE']
z = [5, 6, 1, 1, 2, 14, 7, 1, 3, 6, 1, 2, 13, 1, 3, 11, 4, 2, 1, 6, 1, 50, 18, 5, 2, 4, 1, 1, 4, 16, 15, 4, 10, 4]
layout = dict(geo={'scope': 'africa'})
data = dict(
type='choropleth',
locations=countries,
locationmode='ISO-3',
colorscale='Viridis',
z=z)
map = go.Figure(data=[data], layout=layout)
py.plot(map)
Output is an interactive map with the z value and ISO-3 code displayed when you hover over.
Intended output:
I would like to have the country's name displayed rather than ISO-3 code. I suppose this can be done by passing in the countries' names as the locations and setting locationmode to 'country names'.
Is there a mapping from ISO to country name for the purposes of this? A list/dict/DataFrame of corresponding values within the plotly config, for example? I've had a look but can't find anything.
Thank you
We converted the country name by referring to a two-letter abbreviation from a three-letter abbreviation. The site from which the data was referenced is the following
Country ISO Codes -> Country Names
c_names = []
for c in countries:
for c2,c3 in iso3.items():
if c3 == c:
for v2,v3 in names.items():
if c2 == v2:
c_names.append(v3)
c_names
['Burundi',
'Benin',
'Burkina Faso',
'Botswana',
'Ivory Coast',
'Cameroon',
'Democratic Republic of the Congo',
'Cape Verde',
'Ethiopia',
'Ghana',
'Guinea',
'Gambia',
'Kenya',
'Liberia',
'Lesotho',
'Madagascar',
'Mali',
'Mozambique',
'Mauritius',
'Malawi',
'Niger',
'Nigeria',
'Rwanda',
'Senegal',
'Sierra Leone',
'Somalia',
'Sao Tome and Principe',
'Chad',
'Togo',
'Tanzania',
'Uganda',
'South Africa',
'Zambia',
'Zimbabwe']
Related
I have the following df:
df = pd.DataFrame({"year":[2020,2020,2020,2021,2021,2021,2022,2022, 2022],"region":['europe','USA','africa','europe','USA','africa','europe','USA','africa'],'volume':[1,6,5,3,8,7,6,3,5]})
I wish to convert it to a list of dictionary such that the year would be mentioned only once in each item. Example
[{'year':2020,'europe':1,'USA':6,'africa':5,}...]
when I do:
df.set_index('year').to_dict('records')
I lost the years and the list
Another approach that uses pivot before to_dict(orient='records')
df.pivot(
index='year',
columns='region',
values='volume'
).reset_index().to_dict(orient='records')
#Output:
#[{'year': 2020, 'USA': 6, 'africa': 5, 'europe': 1},
# {'year': 2021, 'USA': 8, 'africa': 7, 'europe': 3},
# {'year': 2022, 'USA': 3, 'africa': 5, 'europe': 6}]
Try:
d = [
{"year": y, **dict(zip(x["region"], x["volume"]))}
for y, x in df.groupby("year")
]
print(d)
Prints:
[
{"year": 2020, "europe": 1, "USA": 6, "africa": 5},
{"year": 2021, "europe": 3, "USA": 8, "africa": 7},
{"year": 2022, "europe": 6, "USA": 3, "africa": 5},
]
you can use groupby on year and then zip region and volume
import pandas as pd
df = pd.DataFrame({"year":[2020,2020,2020,2021,2021,2021,2022,2022, 2022],"region":['europe','USA','africa','europe','USA','africa','europe','USA','africa'],'volume':[1,6,5,3,8,7,6,3,5]})
year_dfs = df.groupby("year")
records = []
for year, year_df in year_dfs:
year_dict = {key: value for key, value in zip(year_df["region"], year_df["volume"])}
year_dict["year"] = year
records.append(year_dict)
""" Answer
[{'europe': 1, 'USA': 6, 'africa': 5, 'year': 2020},
{'europe': 3, 'USA': 8, 'africa': 7, 'year': 2021},
{'europe': 6, 'USA': 3, 'africa': 5, 'year': 2022}]
"""
To break down each step, you could use pivot to group your df to aggregate the years, your columns become countries, and volume becomes your values
df.pivot('year','region','volume')
region USA africa europe
year
2020 6 5 1
2021 8 7 3
2022 3 5 6
To get this into dictionary format you can use the .to_dict('index')
command (in one line)
x = df.pivot('year','region','volume').to_dict('index')
{2020: {'USA': 6, 'africa': 5, 'europe': 1}, 2021: {'USA': 8, 'africa': 7, 'europe': 3}, 2022: {'USA': 3, 'africa': 5, 'europe': 6}}
finally you could use list comprehension to get it into your desired format
output = [dict(x[y], **{'year':y}) for y in x]
[{'USA': 6, 'africa': 5, 'europe': 1, 'year': 2020}, {'USA': 8, 'africa': 7, 'europe': 3, 'year': 2021}, {'USA': 3, 'africa': 5, 'europe': 6, 'year': 2022}]
I want to convert these loops into a list comprehension but I don't know how to do it. Can anyone help me pls?
this is the list i want to convert:
students = ['Tommy', 'Kitty', 'Jessie', 'Chester', 'Curie', 'Darwing', 'Nancy', 'Sue',
'Peter', 'Andrew', 'Karren', 'Charles', 'Nikhil', 'Justin', 'Astha','Victor',
'Samuel', 'Olivia', 'Tony']
assignment = [2, 5, 5, 7, 1, 5, 2, 7, 5, 1, 1, 1, 2, 1, 5, 2, 7, 2, 7]
x = list(zip(students, assignment))
Output = {}
for ke, y in x:
y = "Group {}".format(y)
if y in Output:
Output[y].append((ke))
else:
Output[y] = [(ke)]
print(Output)
this what I have tried:
{Output[y].append((ke)) if y in Output else Output[y]=[(ke)]for ke, y in x}
You could do this with a nested dictionary/list comprehension:
Output = { f'Group {group}' : [ name for name, g in x if g == group ] for group in set(assignment) }
Output:
{
'Group 2': ['Tommy', 'Nancy', 'Nikhil', 'Victor', 'Olivia'],
'Group 5': ['Kitty', 'Jessie', 'Darwing', 'Peter', 'Astha'],
'Group 7': ['Chester', 'Sue', 'Samuel', 'Tony'],
'Group 1': ['Curie', 'Andrew', 'Karren', 'Charles', 'Justin']
}
data1 = {'students': ['Tommy', 'Kitty', 'Jessie', 'Chester', 'Curie', 'Darwing', 'Nancy', 'Sue',
'Peter', 'Andrew', 'Karren', 'Charles', 'Nikhil', 'Justin', 'Astha','Victor',
'Samuel', 'Olivia', 'Tony'],
'assignment': [2, 5, 5, 7, 1, 5, 2, 7, 5, 1, 1, 1, 2, 1, 5, 2, 7, 2, 7]}
df1 = pd.DataFrame(data1)
df1.groupby('assignment')['students'].agg(set).to_dict()
Output
{1: {'Andrew', 'Charles', 'Curie', 'Justin', 'Karren'},
2: {'Nancy', 'Nikhil', 'Olivia', 'Tommy', 'Victor'},
5: {'Astha', 'Darwing', 'Jessie', 'Kitty', 'Peter'},
7: {'Chester', 'Samuel', 'Sue', 'Tony'}}
You want a dict comprehension which will create a dict whose values come from a list comprehension.
itertools.groupby can help:
from itertools import groupby
x = sorted(list(zip(assignment, students)))
out = {f'Group {x}':[z[1] for z in y] for x,y in groupby(x, lambda y:y[0])}
{'Group 1': ['Andrew', 'Charles', 'Curie', 'Justin', 'Karren'], 'Group 2': ['Nancy', 'Nikhil', 'Olivia', 'Tommy', 'Victor'], 'Group 5': ['Astha', 'Darwing', 'Jessie', 'Kitty', 'Peter'], 'Group 7': ['Chester', 'Samuel', 'Sue', 'Tony']}
I want to change the bar color of the state: AZ, CA, FL, NY, OH, and OK. I did it by counting the index; however, I am wondering if I can change the color according to the names of the x ticks.
import matplotlib.pylab as plt
fig=plt.figure(figsize=(10,8), dpi= 90)
lists = sorted(frequency_state.items())
x, y = zip(*lists)
bars = plt.bar(x, y, color = 'grey')
plt.grid()
plt.xticks(rotation = 90)
for i in [2,3,5,23,24,25,31]:
bars[i].set_color('r')
plt.show()
{'FL': 45,
'OK': 37,
'OH': 33,
'NY': 28,
'TX': 27,
'CA': 25,
'AZ': 17,
'GA': 10,
'KY': 9,
'MN': 8,
'MA': 8,
'LA': 8,
'PA': 7,
'ID': 7,
'NJ': 6,
'VA': 6,
'IN': 6,
'MT': 6,
'TN': 5,
'CT': 5,
'NC': 5,
'WI': 5,
'MD': 4,
'IL': 4,
'UT': 3,
'IA': 3,
'MI': 3,
'AR': 2,
'MO': 2,
'SC': 2,
'AL': 2,
'NV': 2,
'OR': 1,
'SD': 1,
'ND': 1}
Here is the graph:
Normalize the value in the colormap you want to display and set it to the desired color of the bar chart.
import matplotlib.pylab as plt
import matplotlib.colors as mcolors
frequency_state = {'FL': 45, 'OK': 37, 'OH': 33, 'NY': 28, 'TX': 27, 'CA': 25, 'AZ': 17, 'GA': 10, 'KY': 9, 'MN': 8,
'MA': 8, 'LA': 8, 'PA': 7, 'ID': 7, 'NJ': 6, 'VA': 6, 'IN': 6, 'MT': 6, 'TN': 5, 'CT': 5, 'NC': 5, 'WI': 5,
'MD': 4, 'IL': 4, 'UT': 3, 'IA': 3, 'MI': 3, 'AR': 2, 'MO': 2, 'SC': 2, 'AL': 2, 'NV': 2, 'OR': 1, 'SD': 1, 'ND': 1}
fig=plt.figure(figsize=(10,8), dpi= 90)
ax = plt.subplot()
colormap = plt.cm.Blues
normalize = mcolors.Normalize(vmin=min(frequency_state.values()), vmax=max(frequency_state.values()))
lists = sorted(frequency_state.items())
x, y = zip(*lists)
bars = plt.bar(x, y, color='grey')
plt.grid()
plt.xticks(rotation = 90)
for i in [2,3,5,23,24,25,31]:
bars[i].set_color(colormap(normalize(lists[i][1])))
plt.show()
pretty new to python so apologies if I'm going about this wrong! I am building a website on Flask that gets information from the fantasy premier league api for me and my friends, and displays the resulting scores by week in a table. I have retrieved the scores and manipulated them such that I have the following array:
[
[{'GameWeek': 1, 'JH Score': 71}, {'GameWeek': 1, 'Harry Score': 70}, {'GameWeek': 1, 'Alex Score': 64}],
[{'GameWeek': 2, 'JH Score': 80}, {'GameWeek': 2, 'Harry Score': 41}, {'GameWeek': 2, 'Alex Score': 52}],
[{'GameWeek': 3, 'JH Score': 40}, {'GameWeek': 3, 'Harry Score': 60}, {'GameWeek': 3, 'Alex Score': 46}],
[{'GameWeek': 4, 'JH Score': 41}, {'GameWeek': 4, 'Harry Score': 29}, {'GameWeek': 4, 'Alex Score': 65}],
[{'GameWeek': 5, 'JH Score': 65}, {'GameWeek': 5, 'Harry Score': 56}, {'GameWeek': 5, 'Alex Score': 65}],
[{'GameWeek': 6, 'JH Score': 63}, {'GameWeek': 6, 'Harry Score': 54}, {'GameWeek': 6, 'Alex Score': 38}],
[{'GameWeek': 7, 'JH Score': 47}, {'GameWeek': 7, 'Harry Score': 65}, {'GameWeek': 7, 'Alex Score': 46}],
[{'GameWeek': 8, 'JH Score': 87}, {'GameWeek': 8, 'Harry Score': 70}, {'GameWeek': 8, 'Alex Score': 88}]
]
I would like to do the following:
Group those key/value pairs by gameweek, i.e.
{'GameWeek': 1, 'JH Score': 71, 'Harry Score': 70, 'Alex Score': 64},
{'GameWeek': 2, 'JH Score': 80, 'Harry Score': 41, 'Alex Score': 52},
etc
Display this information in a table in browser, of the structure
GameWeek JH Score Harry Score Alex Score
1 71 70 64
2 80 41 52
etc
Thank you in advance for your help. Apologies if I have not been clear enough!
Here, try this code below if you re using Python 3.5+:
res = []
for gmwks in arr:
wk = {}
for dic in gmwks:
wk = {**wk, **dic}
res.append(wk)
This will combine all dicts as you wanted.
doc: https://stackoverflow.com/a/26853961/10929089
I have a list containing multiple dictionaries. Each dictionary contains exact 7 keys. But, the values of keys is mix of string and integer. I want to add the values of integer one.
Below, is my list of dictionaries: I want to just add the values of vega, theta, delta, gamma from all the three dictionaries.
[{'option_type': 'Call', 'expiry': datetime.datetime(1993, 3, 4, 15, 20, 26), 'vega': 2, 'notional': 7840, 'delta': 1, 'strike': 520, 'theta': 5, 'gamma': 3}, {'option_type': 'Call', 'expiry': datetime.datetime(1995, 11, 14, 10, 25, 50), 'vega': 2, 'notional': 1930, 'delta': 1, 'strike': 1960, 'theta': 5, 'gamma': 3}, {'option_type': 'Put', 'expiry': datetime.datetime(1993, 3, 7, 17, 2, 30), 'vega': 2, 'notional': 5530, 'delta': 1, 'strike': 1520, 'theta': 5, 'gamma': 3}]
Here is one option:
>>> import datetime
>>> data_set = [{'option_type': 'Call', 'expiry': datetime.datetime(1993, 3, 4, 15, 20, 26), 'vega': 2, 'notional': 7840, 'delta': 1, 'strike': 520, 'theta': 5, 'gamma': 3}, {'option_type': 'Call', 'expiry': datetime.datetime(1995, 11, 14, 10, 25, 50), 'vega': 2, 'notional': 1930, 'delta': 1, 'strike': 1960, 'theta': 5, 'gamma': 3}, {'option_type': 'Put', 'expiry': datetime.datetime(1993, 3, 7, 17, 2, 30), 'vega': 2, 'notional': 5530, 'delta': 1, 'strike': 1520, 'theta': 5, 'gamma': 3}]
>>>
>>> counts = {'vega': 0, 'theta': 0, 'delta': 0, 'gamma': 0}
>>>
>>> for data in data_set:
... for key in counts.keys():
... counts[key] += data[key]
...
>>>
>>> counts
{'gamma': 9, 'vega': 6, 'delta': 3, 'theta': 15}
Simply iterate and use sum() method
sum_vega = sum(i['vega'] for i in give_dict)
sum_theta = sum(i['theta'] for i in give_dict)
sum_delta = sum(i['delta'] for i in give_dict)
sum_gamma = sum(i['gamma'] for i in give_dict)
Use this:
import datetime
my_list=[
{
"vega": 2,
"notional": 7840,
"delta": 1,
"strike": 520,
"theta": 5,
"option_type": "Call",
"gamma": 3,
"expiry": datetime.datetime(1993, 3, 4, 15, 20, 26)
},
{
"vega": 2,
"notional": 1930,
"delta": 1,
"strike": 1960,
"theta": 5,
"option_type": "Call",
"gamma": 3,
"expiry": datetime.datetime(1995, 11, 14, 10, 25, 50)
},
{
"vega": 2,
"notional": 5530,
"delta": 1,
"strike": 1520,
"theta": 5,
"option_type": "Put",
"gamma": 3,
"expiry": datetime.datetime(1993, 3, 7, 17, 2, 30)
}
]
result = {}
for dict_ in my_list:
for key in dict_:
if result.has_key(key):
if isinstance(dict_[key], int):# For integers
result[key] += dict_[key]
#if isinstance(dict_[key], str):# For strings
# result[key] = result[key] + '' + dict_[key]
# We do not sum other types
else:
result[key] = dict_[key]
print result
Well if you already know the keys whose values you want to add, you can simply create a lookup list while iterating through the items and adding them.
Something along these lines:
lookup_count_list = {'vega':0, 'theta':0, 'delta':0, 'gamma':0}
dict_list = [{'option_type': 'Call', 'expiry': datetime.datetime(1993, 3, 4, 15, 20, 26), 'vega': 2, 'notional': 7840, 'delta': 1, 'strike': 520, 'theta': 5, 'gamma': 3}, {'option_type': 'Call', 'expiry': datetime.datetime(1995, 11, 14, 10, 25, 50), 'vega': 2, 'notional': 1930, 'delta': 1, 'strike': 1960, 'theta': 5, 'gamma': 3}, {'option_type': 'Put', 'expiry': datetime.datetime(1993, 3, 7, 17, 2, 30), 'vega': 2, 'notional': 5530, 'delta': 1, 'strike': 1520, 'theta': 5, 'gamma': 3}]
for item in dict_list:
for key, value in item.iteritems():
if key in lookup_count_list.keys():
item[key] += 1
print(lookup_count_list)