<jupyter_start><jupyter_text>Intentional homicide rates in EU countries in 2016<jupyter_code>import os
import folium
import pandas as pd
import numpy as np
from pyjstat import pyjstat
m = folium.Map(location=[51.0295293,13.6980529], tiles='Mapbox Bright',zoom_start=4)

import urllib.request, json 
# For some reason, folium is only able to read 4326 projected geojson
URL_GEOJSON = "https://ec.europa.eu/eurostat/cache/GISCO/distribution/v2/nuts/geojson/NUTS_RG_20M_2016_4326_LEVL_0.geojson"
#URL_GEOJSON = "https://raw.githubusercontent.com/eurostat/Nuts2json/gh-pages/2016/4258/20M/nutsbn_1.json"
with urllib.request.urlopen(URL_GEOJSON) as url:
    geo_json_data = json.loads(url.read().decode())
    
folium.GeoJson(geo_json_data).add_to(m)

#m
dataset_code = "crim_off_cat"
query = dataset_code + "?precision=1&unit=P_HTHAB&time=2016"
# Read the data
DATA_URL="http://ec.europa.eu/eurostat/wdds/rest/data/v2.1/json/en/{:}".format(query)
jstatfile = dataset_code + ".jstat"

newdata = True
if os.path.exists(jstatfile) and not newdata:
    print("Data retrieved from {:}.".format(jstatfile))
    with open(jstatfile, "r") as f:
        dataset = pyjstat.Dataset.read(f.read())
else:
    dataset = pyjstat.Dataset.read(DATA_URL)
    with open(jstatfile, "w") as f:
        f.write(dataset.write())

df = dataset.write('dataframe')
dfbck = df # backup
# Filters
df = df[df.iccs.str.contains("Intentional homicide")]

# Prepare the data
# Missing data is replaced with the latest avaliable 
idx = df[df.geo == "Netherlands"].index[0]
df.iat[idx,4] = 0.74 # 2013 (Eurostat)
idx = df[df.geo == "Belgium"].index[0]
df.iat[idx,4] = 1.96 # 2015 (Eurostat)
idx = df[df.geo == "North Macedonia"].index[0]
df.iat[idx,4] = 1.21 # 2014 (Eurostat)
idx = df[df.geo == "Turkey"].index[0]
df.iat[idx,4] = 2.42 # 2012 (Eurostat)
#df
nuts_name = dataset["dimension"]["geo"]["category"]["label"]
name_nuts = {v: k for k, v in nuts_name.items()}


# Convert to country level
#UK = England + Wales + Scotland + North Ireland
name_nuts["United Kingdom"] = "UK"
s = pd.Series([df.iccs.iloc[0],df.unit.iloc[0],"United Kingdom",df.time.iloc[0],
               np.average([df[df.geo == 'England and Wales'].iloc[0]['value'],
                           df[df.geo == 'Northern Ireland (UK)'].iloc[0]['value'],
                           df[df.geo == 'Scotland'].iloc[0]['value']])    ],
             index = df.columns)
#               3.], index=df.columns)
df = df.append(s, ignore_index=True)
df[df.geo == "United Kingdom"]
cntry_codes = []
for i in df.geo:
    cntry_codes.append(name_nuts[i])

df["cntry"] = cntry_codes
#df.head(30)
folium.Choropleth(
    geo_data=geo_json_data,
    name='Intentional homicides',
    data=df,
    columns=['cntry','value'],
    key_on='feature.id',
    fill_color='YlOrRd',
    fill_opacity=1,
    line_opacity=0.2,
    legend_name='Intentional homicides per 100 000 person (2016)',
).add_to(m)


folium.LayerControl('topleft').add_to(m)
#m
m.save("index.html")
df = df.fillna(0)
highest_rate = df.value.sort_values().tail(8).index

df.iloc[highest_rate[::-1]]
#name_nuts
#df[df.time == '2016']
#geo_json_data
#df<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1662444585180494, 'fraction_numerical': 0.038948701709943, 'mean_word_length': 4.2301324503311255, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '235036', 'license': 'MIT', 'n_tokens_mistral': 1320, 'n_tokens_neox': 1217, 'n_words': 229, 'path': 'eurostat/crim/crim_off_cat/crim_off_cat.ipynb', 'repo_name': 'zyth0s/dataverse', 'size': '3158'}