-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript-ukraine-main.py
More file actions
58 lines (50 loc) · 1.98 KB
/
script-ukraine-main.py
File metadata and controls
58 lines (50 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import pandas as pd
import datawrappergraphics
## The ID of the Ukraine map Datawrapper.
# LIVE CHART ID: wQIM1
# TEST CHART ID: sM21M
UKRAINE_CHART_ID = "wQIM1"
## Import sheet data.
raw = (pd
.read_csv("https://docs.google.com/spreadsheets/d/17RIbkQI6o_Y_NZalfqZvB8n_j_AmTV5GoNMuzdbkw3w/export?format=csv&gid=0", encoding="utf-8")
.dropna(how="all", axis=1)
)
## Rename columns from the spreadsheet.
raw.columns = ["title", "tooltip", "source", "hide_title", "visible", "coordinates", "anchor", "icon"]
## Clean data.
data = (raw
.dropna(how="all")
.set_index("title")
.reset_index()
.loc[raw["visible"] == True]
)
data["source"] = data["source"].fillna("")
data["anchor"] = data["anchor"].str.lower()
data["tooltip"] = data["tooltip"].str.strip()
data["tooltip"] = '<b>' + data["title"] + '</b><br>' + data["tooltip"] + ' <i>(Source: ' + data["source"].fillna("").str.strip().str.replace("\"", "'") + ')</i>'
data["markerColor"] = "#c42127"
data["icon"] = 'circle-sm'
data["scale"] = 1.2
data["type"] = "point"
data["longitude"] = data["coordinates"].apply(lambda x: x.split(", ")[0].replace("[", ""))
data["latitude"] = data["coordinates"].apply(lambda x: x.split(", ")[1].replace("]", ""))
data = data.drop(columns=["coordinates"])
source_list = set(data["source"].to_list())
source_list_clean = []
for entry in source_list:
try:
word = entry.split(", ")
source_list_clean.append(word)
except:
pass
source_list_clean = [item for sublist in source_list_clean for item in sublist]
source_list_clean = [x for x in source_list_clean if x]
source_list_clean = set(source_list_clean)
source_string = ", ".join(source_list_clean)
ukraine = (datawrappergraphics.Map(chart_id=UKRAINE_CHART_ID)
.data(data, append="./assets/shapes/shapes-ukrainemap.json")
.head(f"Russian military invasion in Ukraine")
.deck("")
.footer(note=f"Source: {source_string}.", byline="Wendy Martinez, Dexter McMillan")
.publish()
)