Skip to content

Instantly share code, notes, and snippets.

@minrk
Last active November 17, 2021 18:49
Show Gist options
  • Save minrk/28e6c3aeb9e7a208e0986a67892e912d to your computer and use it in GitHub Desktop.
Save minrk/28e6c3aeb9e7a208e0986a67892e912d to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 91,
"id": "0800a584-84fe-4ecc-9c6e-f2c6c82f51d6",
"metadata": {},
"outputs": [],
"source": [
"# read my matomo api token\n",
"with open(\"matomo-token\") as f:\n",
" token = f.read().strip()"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "f4bbb4ad-5636-4499-8de1-e1639c64b87a",
"metadata": {},
"outputs": [],
"source": [
"url = \"https://mybinder.org/matomo/index.php\"\n"
]
},
{
"cell_type": "code",
"execution_count": 92,
"id": "ace337be-f258-4ac3-85d1-0016abdf033e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'value': '3.14.1'}"
]
},
"execution_count": 92,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import requests\n",
"\n",
"def api_request(method, **params):\n",
" \"\"\"Make a matomo API request\"\"\"\n",
" data={\n",
" \"token_auth\": token,\n",
" \"format\": \"JSON\",\n",
" \"module\": \"API\",\n",
" \"method\": method,\n",
" \"idSite\": \"1\",\n",
" }\n",
" data.update(params)\n",
" r = requests.post(url, data=data)\n",
" try:\n",
" r.raise_for_status()\n",
" except requests.HTTPError as e:\n",
" print(e)\n",
" if r.status_code >= 500:\n",
" # monthly queries seem to time out a lot\n",
" # eventually, the result is cached and it responds quickly\n",
" print(\"retrying\")\n",
" time.sleep(1)\n",
" return api_request(method, **params)\n",
" else:\n",
" raise\n",
" return r.json()\n",
"\n",
"api_request(\"API.getMatomoVersion\")\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 70,
"id": "52bfe017-fa30-4997-8e74-c07d5b855e4f",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"months = {}"
]
},
{
"cell_type": "markdown",
"id": "06451328-f13c-4ff2-a78f-f606feccf3c6",
"metadata": {},
"source": [
"retry a bunch because it seems to time out the first time we request each month,\n",
"but eventually succeed"
]
},
{
"cell_type": "code",
"execution_count": 93,
"id": "f83a819d-eed0-4ba7-84ba-dfb1a8b097ec",
"metadata": {},
"outputs": [],
"source": [
"for month in range(1, 11):\n",
" if month not in months:\n",
" date = f\"2021-{month:02}-01\"\n",
" print(f\"Fetching {date}\")\n",
" records = api_request(\n",
" \"UserCountry.getContinent\",\n",
" period=\"month\",\n",
" date=date,\n",
" showColumns=\"label,nb_visits,sum_daily_nb_uniq_visitors\",\n",
" )\n",
" for record in records:\n",
" record[\"date\"] = date\n",
" months[month] = records"
]
},
{
"cell_type": "code",
"execution_count": 102,
"id": "6551b83c-59cd-471e-b62c-8b8187d5a8f9",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>label</th>\n",
" <th>nb_visits</th>\n",
" <th>sum_daily_nb_uniq_visitors</th>\n",
" <th>code</th>\n",
" <th>date</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>70</th>\n",
" <td>Africa</td>\n",
" <td>664</td>\n",
" <td>529</td>\n",
" <td>Africa</td>\n",
" <td>2021-09-01</td>\n",
" </tr>\n",
" <tr>\n",
" <th>71</th>\n",
" <td>Central America</td>\n",
" <td>129</td>\n",
" <td>101</td>\n",
" <td>Central America</td>\n",
" <td>2021-09-01</td>\n",
" </tr>\n",
" <tr>\n",
" <th>72</th>\n",
" <td>North America</td>\n",
" <td>248606</td>\n",
" <td>204204</td>\n",
" <td>North America</td>\n",
" <td>2021-10-01</td>\n",
" </tr>\n",
" <tr>\n",
" <th>73</th>\n",
" <td>Europe</td>\n",
" <td>160872</td>\n",
" <td>131755</td>\n",
" <td>Europe</td>\n",
" <td>2021-10-01</td>\n",
" </tr>\n",
" <tr>\n",
" <th>74</th>\n",
" <td>Asia</td>\n",
" <td>83917</td>\n",
" <td>68437</td>\n",
" <td>Asia</td>\n",
" <td>2021-10-01</td>\n",
" </tr>\n",
" <tr>\n",
" <th>75</th>\n",
" <td>South America</td>\n",
" <td>15423</td>\n",
" <td>12659</td>\n",
" <td>South America</td>\n",
" <td>2021-10-01</td>\n",
" </tr>\n",
" <tr>\n",
" <th>76</th>\n",
" <td>Unknown</td>\n",
" <td>7102</td>\n",
" <td>5686</td>\n",
" <td>Unknown</td>\n",
" <td>2021-10-01</td>\n",
" </tr>\n",
" <tr>\n",
" <th>77</th>\n",
" <td>Oceania</td>\n",
" <td>1743</td>\n",
" <td>1447</td>\n",
" <td>Oceania</td>\n",
" <td>2021-10-01</td>\n",
" </tr>\n",
" <tr>\n",
" <th>78</th>\n",
" <td>Africa</td>\n",
" <td>829</td>\n",
" <td>665</td>\n",
" <td>Africa</td>\n",
" <td>2021-10-01</td>\n",
" </tr>\n",
" <tr>\n",
" <th>79</th>\n",
" <td>Central America</td>\n",
" <td>99</td>\n",
" <td>76</td>\n",
" <td>Central America</td>\n",
" <td>2021-10-01</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" label nb_visits sum_daily_nb_uniq_visitors code \\\n",
"70 Africa 664 529 Africa \n",
"71 Central America 129 101 Central America \n",
"72 North America 248606 204204 North America \n",
"73 Europe 160872 131755 Europe \n",
"74 Asia 83917 68437 Asia \n",
"75 South America 15423 12659 South America \n",
"76 Unknown 7102 5686 Unknown \n",
"77 Oceania 1743 1447 Oceania \n",
"78 Africa 829 665 Africa \n",
"79 Central America 99 76 Central America \n",
"\n",
" date \n",
"70 2021-09-01 \n",
"71 2021-09-01 \n",
"72 2021-10-01 \n",
"73 2021-10-01 \n",
"74 2021-10-01 \n",
"75 2021-10-01 \n",
"76 2021-10-01 \n",
"77 2021-10-01 \n",
"78 2021-10-01 \n",
"79 2021-10-01 "
]
},
"execution_count": 102,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.tail(10)"
]
},
{
"cell_type": "code",
"execution_count": 99,
"id": "16afe1b4-6d78-47ff-b158-90d9e462dac7",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"<div id=\"altair-viz-360ea1a696944fe69920b5789aafa788\"></div>\n",
"<script type=\"text/javascript\">\n",
" (function(spec, embedOpt){\n",
" let outputDiv = document.currentScript.previousElementSibling;\n",
" if (outputDiv.id !== \"altair-viz-360ea1a696944fe69920b5789aafa788\") {\n",
" outputDiv = document.getElementById(\"altair-viz-360ea1a696944fe69920b5789aafa788\");\n",
" }\n",
" const paths = {\n",
" \"vega\": \"https://cdn.jsdelivr.net/npm//vega@5?noext\",\n",
" \"vega-lib\": \"https://cdn.jsdelivr.net/npm//vega-lib?noext\",\n",
" \"vega-lite\": \"https://cdn.jsdelivr.net/npm//vega-lite@4.8.1?noext\",\n",
" \"vega-embed\": \"https://cdn.jsdelivr.net/npm//vega-embed@6?noext\",\n",
" };\n",
"\n",
" function loadScript(lib) {\n",
" return new Promise(function(resolve, reject) {\n",
" var s = document.createElement('script');\n",
" s.src = paths[lib];\n",
" s.async = true;\n",
" s.onload = () => resolve(paths[lib]);\n",
" s.onerror = () => reject(`Error loading script: ${paths[lib]}`);\n",
" document.getElementsByTagName(\"head\")[0].appendChild(s);\n",
" });\n",
" }\n",
"\n",
" function showError(err) {\n",
" outputDiv.innerHTML = `<div class=\"error\" style=\"color:red;\">${err}</div>`;\n",
" throw err;\n",
" }\n",
"\n",
" function displayChart(vegaEmbed) {\n",
" vegaEmbed(outputDiv, spec, embedOpt)\n",
" .catch(err => showError(`Javascript Error: ${err.message}<br>This usually means there's a typo in your chart specification. See the javascript console for the full traceback.`));\n",
" }\n",
"\n",
" if(typeof define === \"function\" && define.amd) {\n",
" requirejs.config({paths});\n",
" require([\"vega-embed\"], displayChart, err => showError(`Error loading script: ${err.message}`));\n",
" } else if (typeof vegaEmbed === \"function\") {\n",
" displayChart(vegaEmbed);\n",
" } else {\n",
" loadScript(\"vega\")\n",
" .then(() => loadScript(\"vega-lite\"))\n",
" .then(() => loadScript(\"vega-embed\"))\n",
" .catch(showError)\n",
" .then(() => displayChart(vegaEmbed));\n",
" }\n",
" })({\"config\": {\"view\": {\"continuousWidth\": 400, \"continuousHeight\": 300}}, \"data\": {\"name\": \"data-87ea29da526f881fd3c6b436d6be510e\"}, \"mark\": \"line\", \"encoding\": {\"color\": {\"type\": \"nominal\", \"field\": \"label\", \"sort\": {\"encoding\": \"y\", \"order\": \"descending\"}, \"title\": \"Continent\"}, \"x\": {\"type\": \"nominal\", \"field\": \"date\", \"title\": \"Month\"}, \"y\": {\"type\": \"quantitative\", \"field\": \"nb_visits\", \"scale\": {\"type\": \"log\"}, \"title\": \"Monthly visits\"}}, \"selection\": {\"selector021\": {\"type\": \"interval\", \"bind\": \"scales\", \"encodings\": [\"x\", \"y\"]}}, \"$schema\": \"https://vega.github.io/schema/vega-lite/v4.8.1.json\", \"datasets\": {\"data-87ea29da526f881fd3c6b436d6be510e\": [{\"label\": \"North America\", \"nb_visits\": 194257, \"sum_daily_nb_uniq_visitors\": 155873, \"code\": \"North America\", \"date\": \"2021-01-01\"}, {\"label\": \"Europe\", \"nb_visits\": 131855, \"sum_daily_nb_uniq_visitors\": 103127, \"code\": \"Europe\", \"date\": \"2021-01-01\"}, {\"label\": \"Asia\", \"nb_visits\": 48545, \"sum_daily_nb_uniq_visitors\": 39462, \"code\": \"Asia\", \"date\": \"2021-01-01\"}, {\"label\": \"South America\", \"nb_visits\": 8590, \"sum_daily_nb_uniq_visitors\": 6724, \"code\": \"South America\", \"date\": \"2021-01-01\"}, {\"label\": \"Unknown\", \"nb_visits\": 4035, \"sum_daily_nb_uniq_visitors\": 3238, \"code\": \"Unknown\", \"date\": \"2021-01-01\"}, {\"label\": \"Oceania\", \"nb_visits\": 983, \"sum_daily_nb_uniq_visitors\": 825, \"code\": \"Oceania\", \"date\": \"2021-01-01\"}, {\"label\": \"Africa\", \"nb_visits\": 550, \"sum_daily_nb_uniq_visitors\": 436, \"code\": \"Africa\", \"date\": \"2021-01-01\"}, {\"label\": \"Central America\", \"nb_visits\": 86, \"sum_daily_nb_uniq_visitors\": 72, \"code\": \"Central America\", \"date\": \"2021-01-01\"}, {\"label\": \"North America\", \"nb_visits\": 221450, \"sum_daily_nb_uniq_visitors\": 176527, \"code\": \"North America\", \"date\": \"2021-02-01\"}, {\"label\": \"Europe\", \"nb_visits\": 143334, \"sum_daily_nb_uniq_visitors\": 114588, \"code\": \"Europe\", \"date\": \"2021-02-01\"}, {\"label\": \"Asia\", \"nb_visits\": 40614, \"sum_daily_nb_uniq_visitors\": 33062, \"code\": \"Asia\", \"date\": \"2021-02-01\"}, {\"label\": \"South America\", \"nb_visits\": 11278, \"sum_daily_nb_uniq_visitors\": 8577, \"code\": \"South America\", \"date\": \"2021-02-01\"}, {\"label\": \"Unknown\", \"nb_visits\": 4832, \"sum_daily_nb_uniq_visitors\": 3923, \"code\": \"Unknown\", \"date\": \"2021-02-01\"}, {\"label\": \"Oceania\", \"nb_visits\": 1056, \"sum_daily_nb_uniq_visitors\": 894, \"code\": \"Oceania\", \"date\": \"2021-02-01\"}, {\"label\": \"Africa\", \"nb_visits\": 440, \"sum_daily_nb_uniq_visitors\": 349, \"code\": \"Africa\", \"date\": \"2021-02-01\"}, {\"label\": \"Central America\", \"nb_visits\": 101, \"sum_daily_nb_uniq_visitors\": 87, \"code\": \"Central America\", \"date\": \"2021-02-01\"}, {\"label\": \"North America\", \"nb_visits\": 266684, \"sum_daily_nb_uniq_visitors\": 213754, \"code\": \"North America\", \"date\": \"2021-03-01\"}, {\"label\": \"Europe\", \"nb_visits\": 186893, \"sum_daily_nb_uniq_visitors\": 149681, \"code\": \"Europe\", \"date\": \"2021-03-01\"}, {\"label\": \"Asia\", \"nb_visits\": 67536, \"sum_daily_nb_uniq_visitors\": 56199, \"code\": \"Asia\", \"date\": \"2021-03-01\"}, {\"label\": \"South America\", \"nb_visits\": 14972, \"sum_daily_nb_uniq_visitors\": 11643, \"code\": \"South America\", \"date\": \"2021-03-01\"}, {\"label\": \"Unknown\", \"nb_visits\": 6423, \"sum_daily_nb_uniq_visitors\": 5190, \"code\": \"Unknown\", \"date\": \"2021-03-01\"}, {\"label\": \"Oceania\", \"nb_visits\": 1832, \"sum_daily_nb_uniq_visitors\": 1497, \"code\": \"Oceania\", \"date\": \"2021-03-01\"}, {\"label\": \"Africa\", \"nb_visits\": 537, \"sum_daily_nb_uniq_visitors\": 424, \"code\": \"Africa\", \"date\": \"2021-03-01\"}, {\"label\": \"Central America\", \"nb_visits\": 134, \"sum_daily_nb_uniq_visitors\": 104, \"code\": \"Central America\", \"date\": \"2021-03-01\"}, {\"label\": \"North America\", \"nb_visits\": 243889, \"sum_daily_nb_uniq_visitors\": 194968, \"code\": \"North America\", \"date\": \"2021-04-01\"}, {\"label\": \"Europe\", \"nb_visits\": 165829, \"sum_daily_nb_uniq_visitors\": 133277, \"code\": \"Europe\", \"date\": \"2021-04-01\"}, {\"label\": \"Asia\", \"nb_visits\": 66513, \"sum_daily_nb_uniq_visitors\": 54508, \"code\": \"Asia\", \"date\": \"2021-04-01\"}, {\"label\": \"South America\", \"nb_visits\": 16084, \"sum_daily_nb_uniq_visitors\": 12610, \"code\": \"South America\", \"date\": \"2021-04-01\"}, {\"label\": \"Unknown\", \"nb_visits\": 7890, \"sum_daily_nb_uniq_visitors\": 6079, \"code\": \"Unknown\", \"date\": \"2021-04-01\"}, {\"label\": \"Oceania\", \"nb_visits\": 1775, \"sum_daily_nb_uniq_visitors\": 1437, \"code\": \"Oceania\", \"date\": \"2021-04-01\"}, {\"label\": \"Africa\", \"nb_visits\": 520, \"sum_daily_nb_uniq_visitors\": 427, \"code\": \"Africa\", \"date\": \"2021-04-01\"}, {\"label\": \"Central America\", \"nb_visits\": 121, \"sum_daily_nb_uniq_visitors\": 105, \"code\": \"Central America\", \"date\": \"2021-04-01\"}, {\"label\": \"North America\", \"nb_visits\": 225208, \"sum_daily_nb_uniq_visitors\": 181547, \"code\": \"North America\", \"date\": \"2021-05-01\"}, {\"label\": \"Europe\", \"nb_visits\": 147872, \"sum_daily_nb_uniq_visitors\": 118161, \"code\": \"Europe\", \"date\": \"2021-05-01\"}, {\"label\": \"Asia\", \"nb_visits\": 64453, \"sum_daily_nb_uniq_visitors\": 52850, \"code\": \"Asia\", \"date\": \"2021-05-01\"}, {\"label\": \"South America\", \"nb_visits\": 15065, \"sum_daily_nb_uniq_visitors\": 12046, \"code\": \"South America\", \"date\": \"2021-05-01\"}, {\"label\": \"Unknown\", \"nb_visits\": 6991, \"sum_daily_nb_uniq_visitors\": 5485, \"code\": \"Unknown\", \"date\": \"2021-05-01\"}, {\"label\": \"Oceania\", \"nb_visits\": 1669, \"sum_daily_nb_uniq_visitors\": 1351, \"code\": \"Oceania\", \"date\": \"2021-05-01\"}, {\"label\": \"Africa\", \"nb_visits\": 646, \"sum_daily_nb_uniq_visitors\": 500, \"code\": \"Africa\", \"date\": \"2021-05-01\"}, {\"label\": \"Central America\", \"nb_visits\": 126, \"sum_daily_nb_uniq_visitors\": 96, \"code\": \"Central America\", \"date\": \"2021-05-01\"}, {\"label\": \"North America\", \"nb_visits\": 213772, \"sum_daily_nb_uniq_visitors\": 173073, \"code\": \"North America\", \"date\": \"2021-06-01\"}, {\"label\": \"Europe\", \"nb_visits\": 119218, \"sum_daily_nb_uniq_visitors\": 94650, \"code\": \"Europe\", \"date\": \"2021-06-01\"}, {\"label\": \"Asia\", \"nb_visits\": 62829, \"sum_daily_nb_uniq_visitors\": 50619, \"code\": \"Asia\", \"date\": \"2021-06-01\"}, {\"label\": \"South America\", \"nb_visits\": 15874, \"sum_daily_nb_uniq_visitors\": 12631, \"code\": \"South America\", \"date\": \"2021-06-01\"}, {\"label\": \"Unknown\", \"nb_visits\": 5783, \"sum_daily_nb_uniq_visitors\": 4657, \"code\": \"Unknown\", \"date\": \"2021-06-01\"}, {\"label\": \"Oceania\", \"nb_visits\": 1625, \"sum_daily_nb_uniq_visitors\": 1316, \"code\": \"Oceania\", \"date\": \"2021-06-01\"}, {\"label\": \"Africa\", \"nb_visits\": 637, \"sum_daily_nb_uniq_visitors\": 496, \"code\": \"Africa\", \"date\": \"2021-06-01\"}, {\"label\": \"Central America\", \"nb_visits\": 80, \"sum_daily_nb_uniq_visitors\": 67, \"code\": \"Central America\", \"date\": \"2021-06-01\"}, {\"label\": \"North America\", \"nb_visits\": 199248, \"sum_daily_nb_uniq_visitors\": 161424, \"code\": \"North America\", \"date\": \"2021-07-01\"}, {\"label\": \"Europe\", \"nb_visits\": 93324, \"sum_daily_nb_uniq_visitors\": 75941, \"code\": \"Europe\", \"date\": \"2021-07-01\"}, {\"label\": \"Asia\", \"nb_visits\": 56083, \"sum_daily_nb_uniq_visitors\": 45660, \"code\": \"Asia\", \"date\": \"2021-07-01\"}, {\"label\": \"South America\", \"nb_visits\": 14937, \"sum_daily_nb_uniq_visitors\": 11731, \"code\": \"South America\", \"date\": \"2021-07-01\"}, {\"label\": \"Unknown\", \"nb_visits\": 4639, \"sum_daily_nb_uniq_visitors\": 3842, \"code\": \"Unknown\", \"date\": \"2021-07-01\"}, {\"label\": \"Oceania\", \"nb_visits\": 1584, \"sum_daily_nb_uniq_visitors\": 1333, \"code\": \"Oceania\", \"date\": \"2021-07-01\"}, {\"label\": \"Africa\", \"nb_visits\": 466, \"sum_daily_nb_uniq_visitors\": 376, \"code\": \"Africa\", \"date\": \"2021-07-01\"}, {\"label\": \"Central America\", \"nb_visits\": 131, \"sum_daily_nb_uniq_visitors\": 88, \"code\": \"Central America\", \"date\": \"2021-07-01\"}, {\"label\": \"North America\", \"nb_visits\": 213809, \"sum_daily_nb_uniq_visitors\": 173673, \"code\": \"North America\", \"date\": \"2021-08-01\"}, {\"label\": \"Europe\", \"nb_visits\": 94277, \"sum_daily_nb_uniq_visitors\": 77294, \"code\": \"Europe\", \"date\": \"2021-08-01\"}, {\"label\": \"Asia\", \"nb_visits\": 55442, \"sum_daily_nb_uniq_visitors\": 45839, \"code\": \"Asia\", \"date\": \"2021-08-01\"}, {\"label\": \"South America\", \"nb_visits\": 17293, \"sum_daily_nb_uniq_visitors\": 13899, \"code\": \"South America\", \"date\": \"2021-08-01\"}, {\"label\": \"Unknown\", \"nb_visits\": 6283, \"sum_daily_nb_uniq_visitors\": 5078, \"code\": \"Unknown\", \"date\": \"2021-08-01\"}, {\"label\": \"Oceania\", \"nb_visits\": 1994, \"sum_daily_nb_uniq_visitors\": 1656, \"code\": \"Oceania\", \"date\": \"2021-08-01\"}, {\"label\": \"Africa\", \"nb_visits\": 891, \"sum_daily_nb_uniq_visitors\": 659, \"code\": \"Africa\", \"date\": \"2021-08-01\"}, {\"label\": \"Central America\", \"nb_visits\": 126, \"sum_daily_nb_uniq_visitors\": 105, \"code\": \"Central America\", \"date\": \"2021-08-01\"}, {\"label\": \"North America\", \"nb_visits\": 260938, \"sum_daily_nb_uniq_visitors\": 212361, \"code\": \"North America\", \"date\": \"2021-09-01\"}, {\"label\": \"Europe\", \"nb_visits\": 135679, \"sum_daily_nb_uniq_visitors\": 111263, \"code\": \"Europe\", \"date\": \"2021-09-01\"}, {\"label\": \"Asia\", \"nb_visits\": 76007, \"sum_daily_nb_uniq_visitors\": 62847, \"code\": \"Asia\", \"date\": \"2021-09-01\"}, {\"label\": \"South America\", \"nb_visits\": 15734, \"sum_daily_nb_uniq_visitors\": 12421, \"code\": \"South America\", \"date\": \"2021-09-01\"}, {\"label\": \"Unknown\", \"nb_visits\": 6943, \"sum_daily_nb_uniq_visitors\": 5531, \"code\": \"Unknown\", \"date\": \"2021-09-01\"}, {\"label\": \"Oceania\", \"nb_visits\": 1762, \"sum_daily_nb_uniq_visitors\": 1472, \"code\": \"Oceania\", \"date\": \"2021-09-01\"}, {\"label\": \"Africa\", \"nb_visits\": 664, \"sum_daily_nb_uniq_visitors\": 529, \"code\": \"Africa\", \"date\": \"2021-09-01\"}, {\"label\": \"Central America\", \"nb_visits\": 129, \"sum_daily_nb_uniq_visitors\": 101, \"code\": \"Central America\", \"date\": \"2021-09-01\"}, {\"label\": \"North America\", \"nb_visits\": 248606, \"sum_daily_nb_uniq_visitors\": 204204, \"code\": \"North America\", \"date\": \"2021-10-01\"}, {\"label\": \"Europe\", \"nb_visits\": 160872, \"sum_daily_nb_uniq_visitors\": 131755, \"code\": \"Europe\", \"date\": \"2021-10-01\"}, {\"label\": \"Asia\", \"nb_visits\": 83917, \"sum_daily_nb_uniq_visitors\": 68437, \"code\": \"Asia\", \"date\": \"2021-10-01\"}, {\"label\": \"South America\", \"nb_visits\": 15423, \"sum_daily_nb_uniq_visitors\": 12659, \"code\": \"South America\", \"date\": \"2021-10-01\"}, {\"label\": \"Unknown\", \"nb_visits\": 7102, \"sum_daily_nb_uniq_visitors\": 5686, \"code\": \"Unknown\", \"date\": \"2021-10-01\"}, {\"label\": \"Oceania\", \"nb_visits\": 1743, \"sum_daily_nb_uniq_visitors\": 1447, \"code\": \"Oceania\", \"date\": \"2021-10-01\"}, {\"label\": \"Africa\", \"nb_visits\": 829, \"sum_daily_nb_uniq_visitors\": 665, \"code\": \"Africa\", \"date\": \"2021-10-01\"}, {\"label\": \"Central America\", \"nb_visits\": 99, \"sum_daily_nb_uniq_visitors\": 76, \"code\": \"Central America\", \"date\": \"2021-10-01\"}]}}, {\"mode\": \"vega-lite\"});\n",
"</script>"
],
"text/plain": [
"alt.Chart(...)"
]
},
"execution_count": 99,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from itertools import chain\n",
"\n",
"import altair as alt\n",
"\n",
"df = pd.DataFrame(chain(*months.values()))\n",
"df.head()\n",
"\n",
"alt.Chart(df).mark_line().encode(\n",
" color=alt.Color(\n",
" \"label\", title=\"Continent\", sort={\"encoding\": \"y\", \"order\": \"descending\"}\n",
" ),\n",
" x=alt.Y(\"date\", title=\"Month\"),\n",
" y=alt.Y(\n",
" \"nb_visits\",\n",
" title=\"Monthly visits\",\n",
" scale=alt.Scale(type=\"log\"),\n",
" ),\n",
").interactive()"
]
},
{
"cell_type": "markdown",
"id": "48f61894-c5bb-439b-9951-045e2a0ef125",
"metadata": {},
"source": [
"And again, summing all non-NA-EU together,\n",
"ignoring \"Unknown\""
]
},
{
"cell_type": "code",
"execution_count": 117,
"id": "49443c49-7608-419a-968a-777cef435bf9",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/minrk/conda/lib/python3.9/site-packages/pandas/core/frame.py:3607: SettingWithCopyWarning: \n",
"A value is trying to be set on a copy of a slice from a DataFrame.\n",
"Try using .loc[row_indexer,col_indexer] = value instead\n",
"\n",
"See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
" self._set_item(key, value)\n",
"/Users/minrk/conda/lib/python3.9/site-packages/pandas/core/indexing.py:1817: SettingWithCopyWarning: \n",
"A value is trying to be set on a copy of a slice from a DataFrame.\n",
"Try using .loc[row_indexer,col_indexer] = value instead\n",
"\n",
"See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
" self._setitem_single_column(loc, value, pi)\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>date</th>\n",
" <th>Gross Region</th>\n",
" <th>nb_visits</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>2021-01-01</td>\n",
" <td>Europe</td>\n",
" <td>131855</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2021-01-01</td>\n",
" <td>North America</td>\n",
" <td>194257</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2021-01-01</td>\n",
" <td>Rest of World</td>\n",
" <td>58754</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2021-02-01</td>\n",
" <td>Europe</td>\n",
" <td>143334</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2021-02-01</td>\n",
" <td>North America</td>\n",
" <td>221450</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>2021-02-01</td>\n",
" <td>Rest of World</td>\n",
" <td>53489</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>2021-03-01</td>\n",
" <td>Europe</td>\n",
" <td>186893</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>2021-03-01</td>\n",
" <td>North America</td>\n",
" <td>266684</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>2021-03-01</td>\n",
" <td>Rest of World</td>\n",
" <td>85011</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>2021-04-01</td>\n",
" <td>Europe</td>\n",
" <td>165829</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>2021-04-01</td>\n",
" <td>North America</td>\n",
" <td>243889</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>2021-04-01</td>\n",
" <td>Rest of World</td>\n",
" <td>85013</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>2021-05-01</td>\n",
" <td>Europe</td>\n",
" <td>147872</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>2021-05-01</td>\n",
" <td>North America</td>\n",
" <td>225208</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>2021-05-01</td>\n",
" <td>Rest of World</td>\n",
" <td>81959</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
" <td>2021-06-01</td>\n",
" <td>Europe</td>\n",
" <td>119218</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16</th>\n",
" <td>2021-06-01</td>\n",
" <td>North America</td>\n",
" <td>213772</td>\n",
" </tr>\n",
" <tr>\n",
" <th>17</th>\n",
" <td>2021-06-01</td>\n",
" <td>Rest of World</td>\n",
" <td>81045</td>\n",
" </tr>\n",
" <tr>\n",
" <th>18</th>\n",
" <td>2021-07-01</td>\n",
" <td>Europe</td>\n",
" <td>93324</td>\n",
" </tr>\n",
" <tr>\n",
" <th>19</th>\n",
" <td>2021-07-01</td>\n",
" <td>North America</td>\n",
" <td>199248</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20</th>\n",
" <td>2021-07-01</td>\n",
" <td>Rest of World</td>\n",
" <td>73201</td>\n",
" </tr>\n",
" <tr>\n",
" <th>21</th>\n",
" <td>2021-08-01</td>\n",
" <td>Europe</td>\n",
" <td>94277</td>\n",
" </tr>\n",
" <tr>\n",
" <th>22</th>\n",
" <td>2021-08-01</td>\n",
" <td>North America</td>\n",
" <td>213809</td>\n",
" </tr>\n",
" <tr>\n",
" <th>23</th>\n",
" <td>2021-08-01</td>\n",
" <td>Rest of World</td>\n",
" <td>75746</td>\n",
" </tr>\n",
" <tr>\n",
" <th>24</th>\n",
" <td>2021-09-01</td>\n",
" <td>Europe</td>\n",
" <td>135679</td>\n",
" </tr>\n",
" <tr>\n",
" <th>25</th>\n",
" <td>2021-09-01</td>\n",
" <td>North America</td>\n",
" <td>260938</td>\n",
" </tr>\n",
" <tr>\n",
" <th>26</th>\n",
" <td>2021-09-01</td>\n",
" <td>Rest of World</td>\n",
" <td>94296</td>\n",
" </tr>\n",
" <tr>\n",
" <th>27</th>\n",
" <td>2021-10-01</td>\n",
" <td>Europe</td>\n",
" <td>160872</td>\n",
" </tr>\n",
" <tr>\n",
" <th>28</th>\n",
" <td>2021-10-01</td>\n",
" <td>North America</td>\n",
" <td>248606</td>\n",
" </tr>\n",
" <tr>\n",
" <th>29</th>\n",
" <td>2021-10-01</td>\n",
" <td>Rest of World</td>\n",
" <td>102011</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" date Gross Region nb_visits\n",
"0 2021-01-01 Europe 131855\n",
"1 2021-01-01 North America 194257\n",
"2 2021-01-01 Rest of World 58754\n",
"3 2021-02-01 Europe 143334\n",
"4 2021-02-01 North America 221450\n",
"5 2021-02-01 Rest of World 53489\n",
"6 2021-03-01 Europe 186893\n",
"7 2021-03-01 North America 266684\n",
"8 2021-03-01 Rest of World 85011\n",
"9 2021-04-01 Europe 165829\n",
"10 2021-04-01 North America 243889\n",
"11 2021-04-01 Rest of World 85013\n",
"12 2021-05-01 Europe 147872\n",
"13 2021-05-01 North America 225208\n",
"14 2021-05-01 Rest of World 81959\n",
"15 2021-06-01 Europe 119218\n",
"16 2021-06-01 North America 213772\n",
"17 2021-06-01 Rest of World 81045\n",
"18 2021-07-01 Europe 93324\n",
"19 2021-07-01 North America 199248\n",
"20 2021-07-01 Rest of World 73201\n",
"21 2021-08-01 Europe 94277\n",
"22 2021-08-01 North America 213809\n",
"23 2021-08-01 Rest of World 75746\n",
"24 2021-09-01 Europe 135679\n",
"25 2021-09-01 North America 260938\n",
"26 2021-09-01 Rest of World 94296\n",
"27 2021-10-01 Europe 160872\n",
"28 2021-10-01 North America 248606\n",
"29 2021-10-01 Rest of World 102011"
]
},
"execution_count": 117,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gross = df[df.label != \"Unknown\"]\n",
"gross[\"Gross Region\"] = gross.label\n",
"\n",
"gross.loc[~gross.label.isin([\"North America\", \"Europe\"]), \"Gross Region\"] = \"Rest of World\"\n",
"\n",
"# regroup sum by gross region\n",
"gross = gross.groupby([\"date\", \"Gross Region\"]).nb_visits.sum().reset_index()\n",
"gross"
]
},
{
"cell_type": "code",
"execution_count": 118,
"id": "38928296-8b4d-4664-8ffa-cfc20bd61295",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"<div id=\"altair-viz-240dc6a5a3414367899941e8e2ca6b17\"></div>\n",
"<script type=\"text/javascript\">\n",
" (function(spec, embedOpt){\n",
" let outputDiv = document.currentScript.previousElementSibling;\n",
" if (outputDiv.id !== \"altair-viz-240dc6a5a3414367899941e8e2ca6b17\") {\n",
" outputDiv = document.getElementById(\"altair-viz-240dc6a5a3414367899941e8e2ca6b17\");\n",
" }\n",
" const paths = {\n",
" \"vega\": \"https://cdn.jsdelivr.net/npm//vega@5?noext\",\n",
" \"vega-lib\": \"https://cdn.jsdelivr.net/npm//vega-lib?noext\",\n",
" \"vega-lite\": \"https://cdn.jsdelivr.net/npm//vega-lite@4.8.1?noext\",\n",
" \"vega-embed\": \"https://cdn.jsdelivr.net/npm//vega-embed@6?noext\",\n",
" };\n",
"\n",
" function loadScript(lib) {\n",
" return new Promise(function(resolve, reject) {\n",
" var s = document.createElement('script');\n",
" s.src = paths[lib];\n",
" s.async = true;\n",
" s.onload = () => resolve(paths[lib]);\n",
" s.onerror = () => reject(`Error loading script: ${paths[lib]}`);\n",
" document.getElementsByTagName(\"head\")[0].appendChild(s);\n",
" });\n",
" }\n",
"\n",
" function showError(err) {\n",
" outputDiv.innerHTML = `<div class=\"error\" style=\"color:red;\">${err}</div>`;\n",
" throw err;\n",
" }\n",
"\n",
" function displayChart(vegaEmbed) {\n",
" vegaEmbed(outputDiv, spec, embedOpt)\n",
" .catch(err => showError(`Javascript Error: ${err.message}<br>This usually means there's a typo in your chart specification. See the javascript console for the full traceback.`));\n",
" }\n",
"\n",
" if(typeof define === \"function\" && define.amd) {\n",
" requirejs.config({paths});\n",
" require([\"vega-embed\"], displayChart, err => showError(`Error loading script: ${err.message}`));\n",
" } else if (typeof vegaEmbed === \"function\") {\n",
" displayChart(vegaEmbed);\n",
" } else {\n",
" loadScript(\"vega\")\n",
" .then(() => loadScript(\"vega-lite\"))\n",
" .then(() => loadScript(\"vega-embed\"))\n",
" .catch(showError)\n",
" .then(() => displayChart(vegaEmbed));\n",
" }\n",
" })({\"config\": {\"view\": {\"continuousWidth\": 400, \"continuousHeight\": 300}}, \"data\": {\"name\": \"data-1eff54db308600a3728150946fd89ebd\"}, \"mark\": \"line\", \"encoding\": {\"color\": {\"type\": \"nominal\", \"field\": \"Gross Region\", \"sort\": {\"encoding\": \"y\", \"order\": \"descending\"}}, \"tooltip\": [{\"type\": \"nominal\", \"field\": \"Gross Region\"}, {\"type\": \"nominal\", \"field\": \"date\"}, {\"type\": \"quantitative\", \"field\": \"nb_visits\"}], \"x\": {\"type\": \"nominal\", \"field\": \"date\", \"title\": \"Month\"}, \"y\": {\"type\": \"quantitative\", \"field\": \"nb_visits\", \"scale\": {\"type\": \"log\"}, \"title\": \"Monthly visits\"}}, \"selection\": {\"selector025\": {\"type\": \"interval\", \"bind\": \"scales\", \"encodings\": [\"x\", \"y\"]}}, \"$schema\": \"https://vega.github.io/schema/vega-lite/v4.8.1.json\", \"datasets\": {\"data-1eff54db308600a3728150946fd89ebd\": [{\"date\": \"2021-01-01\", \"Gross Region\": \"Europe\", \"nb_visits\": 131855}, {\"date\": \"2021-01-01\", \"Gross Region\": \"North America\", \"nb_visits\": 194257}, {\"date\": \"2021-01-01\", \"Gross Region\": \"Rest of World\", \"nb_visits\": 58754}, {\"date\": \"2021-02-01\", \"Gross Region\": \"Europe\", \"nb_visits\": 143334}, {\"date\": \"2021-02-01\", \"Gross Region\": \"North America\", \"nb_visits\": 221450}, {\"date\": \"2021-02-01\", \"Gross Region\": \"Rest of World\", \"nb_visits\": 53489}, {\"date\": \"2021-03-01\", \"Gross Region\": \"Europe\", \"nb_visits\": 186893}, {\"date\": \"2021-03-01\", \"Gross Region\": \"North America\", \"nb_visits\": 266684}, {\"date\": \"2021-03-01\", \"Gross Region\": \"Rest of World\", \"nb_visits\": 85011}, {\"date\": \"2021-04-01\", \"Gross Region\": \"Europe\", \"nb_visits\": 165829}, {\"date\": \"2021-04-01\", \"Gross Region\": \"North America\", \"nb_visits\": 243889}, {\"date\": \"2021-04-01\", \"Gross Region\": \"Rest of World\", \"nb_visits\": 85013}, {\"date\": \"2021-05-01\", \"Gross Region\": \"Europe\", \"nb_visits\": 147872}, {\"date\": \"2021-05-01\", \"Gross Region\": \"North America\", \"nb_visits\": 225208}, {\"date\": \"2021-05-01\", \"Gross Region\": \"Rest of World\", \"nb_visits\": 81959}, {\"date\": \"2021-06-01\", \"Gross Region\": \"Europe\", \"nb_visits\": 119218}, {\"date\": \"2021-06-01\", \"Gross Region\": \"North America\", \"nb_visits\": 213772}, {\"date\": \"2021-06-01\", \"Gross Region\": \"Rest of World\", \"nb_visits\": 81045}, {\"date\": \"2021-07-01\", \"Gross Region\": \"Europe\", \"nb_visits\": 93324}, {\"date\": \"2021-07-01\", \"Gross Region\": \"North America\", \"nb_visits\": 199248}, {\"date\": \"2021-07-01\", \"Gross Region\": \"Rest of World\", \"nb_visits\": 73201}, {\"date\": \"2021-08-01\", \"Gross Region\": \"Europe\", \"nb_visits\": 94277}, {\"date\": \"2021-08-01\", \"Gross Region\": \"North America\", \"nb_visits\": 213809}, {\"date\": \"2021-08-01\", \"Gross Region\": \"Rest of World\", \"nb_visits\": 75746}, {\"date\": \"2021-09-01\", \"Gross Region\": \"Europe\", \"nb_visits\": 135679}, {\"date\": \"2021-09-01\", \"Gross Region\": \"North America\", \"nb_visits\": 260938}, {\"date\": \"2021-09-01\", \"Gross Region\": \"Rest of World\", \"nb_visits\": 94296}, {\"date\": \"2021-10-01\", \"Gross Region\": \"Europe\", \"nb_visits\": 160872}, {\"date\": \"2021-10-01\", \"Gross Region\": \"North America\", \"nb_visits\": 248606}, {\"date\": \"2021-10-01\", \"Gross Region\": \"Rest of World\", \"nb_visits\": 102011}]}}, {\"mode\": \"vega-lite\"});\n",
"</script>"
],
"text/plain": [
"alt.Chart(...)"
]
},
"execution_count": 118,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"alt.Chart(gross).mark_line().encode(\n",
" color=alt.Color(\n",
" \"Gross Region\", sort={\"encoding\": \"y\", \"order\": \"descending\"}\n",
" ),\n",
" x=alt.Y(\"date\", title=\"Month\"),\n",
" y=alt.Y(\n",
" \"nb_visits\",\n",
" title=\"Monthly visits\",\n",
" scale=alt.Scale(type=\"log\"),\n",
" ),\n",
" tooltip=[\n",
" \"Gross Region\",\n",
" \"date\",\n",
" \"nb_visits\",\n",
" ],\n",
").interactive()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {},
"version_major": 2,
"version_minor": 0
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment