Carbon-Insight/notebook/carbon_emission.ipynb

1729 строки
638 KiB
Plaintext
Исходник Обычный вид История

2021-12-31 12:46:06 +03:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Carbon Insight: Carbon Emissions Visualization\n",
"==============================================\n",
"\n",
"This tutorial aims to showcase how to visualize anthropogenic CO2 emissions with a near-global coverage and track correlations between global carbon emissions and socioeconomic factors such as COVID-19 and GDP."
]
},
{
"cell_type": "code",
2022-02-07 09:54:49 +03:00
"execution_count": 1,
2021-12-31 12:46:06 +03:00
"metadata": {},
"outputs": [],
"source": [
"# Requirements\n",
2022-01-25 08:34:45 +03:00
"%pip install numpy\n",
"%pip install pandas\n",
"%pip install matplotlib\n"
2021-12-31 12:46:06 +03:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# A. Process Carbon Emission Data\n",
"\n",
"This notebook helps you to process and visualize carbon emission data provided by [Carbon-Monitor](https://carbonmonitor.org/), which records human-caused carbon emissions from different countries, sources, and timeframes that are of interest to you.\n",
"\n",
"Overview:\n",
"- [Process carbon emission data](#a1)\n",
" - [Download data from Carbon Monitor](#a11)\n",
" - [Calculate the rate of change](#a12)\n",
" - [Expand country regions](#a13)\n",
"- [Visualize carbon emission data](#a2)\n",
" - [Observe carbon emission data from the perspective of time](#a21)\n",
" - [Compare carbon emission data of different sectors](#a22)\n",
"- [Examples](#a3)\n",
" - [World carbon emission data](#a31)\n",
" - [US carbon emission data](#a32)"
]
},
{
"cell_type": "code",
2022-01-25 08:34:45 +03:00
"execution_count": 2,
2021-12-31 12:46:06 +03:00
"metadata": {},
"outputs": [],
"source": [
"import io\n",
"from urllib.request import urlopen\n",
"\n",
"import numpy as np\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"from pandas.plotting import register_matplotlib_converters\n",
"\n",
"register_matplotlib_converters()\n"
]
},
2022-01-04 10:58:46 +03:00
{
"cell_type": "code",
2022-01-25 08:34:45 +03:00
"execution_count": 3,
2022-01-04 10:58:46 +03:00
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"# Optional Function: Export Data\n",
"def export_data(file_name: str, df: pd.DataFrame):\n",
2022-01-25 08:34:45 +03:00
" # df = country_region_name_to_code(df)\n",
2022-01-04 10:58:46 +03:00
" export_path = os.path.join('export_data', file_name)\n",
" print(f'Export Data to {export_path}')\n",
" if not os.path.exists('export_data'):\n",
" os.mkdir('export_data')\n",
2022-01-25 08:34:45 +03:00
" with open(export_path, 'w', encoding='utf-8') as f:\n",
2022-01-04 10:58:46 +03:00
" f.write(df.to_csv(index=None, line_terminator='\\n', encoding='utf-8'))\n"
]
},
2021-12-31 12:46:06 +03:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## <a id=\"a1\"></a> 1. Process Data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <a id=\"a11\"></a> 1.1. Download data from Carbon Monitor\n",
"We are going to download tabular carbon emission data and convert to Pandas Dataframe.\n",
"\n",
"Supported data types:\n",
"\n",
"- ```carbon_global``` includes carbon emission data of 11 countries and regions worldwide.\n",
"- ```carbon_us``` includes carbon emission data of 51 states of the United States.\n",
"- ```carbon_eu``` includes carbon emission data of 27 countries of the European Union.\n",
"- ```carbon_china``` includes carbon emission data of 31 cities and provinces of China."
]
},
{
"cell_type": "code",
2022-01-25 08:34:45 +03:00
"execution_count": 4,
2021-12-31 12:46:06 +03:00
"metadata": {},
"outputs": [],
"source": [
"def get_data_from_carbon_monitor(data_type='carbon_global'):\n",
" assert data_type in ['carbon_global', 'carbon_us', 'carbon_eu', 'carbon_china']\n",
" data_url = f'https://datas.carbonmonitor.org/API/downloadFullDataset.php?source={data_type}'\n",
" data = urlopen(data_url).read().decode('utf-8-sig')\n",
" df = pd.read_csv(io.StringIO(data))\n",
" df = df.drop(columns=['timestamp'])\n",
" df = df.loc[pd.notna(df['date'])]\n",
" df = df.rename(columns={'country': 'country_region'})\n",
" df['date'] = pd.to_datetime(df['date'], format='%d/%m/%Y')\n",
" if data_type == 'carbon_us':\n",
" df = df.loc[df['state'] != 'United States']\n",
" return df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <a id=\"a12\"></a> 1.2. Calculate the rate of change\n",
"The rate of change $\\Delta(s, r, t)$ is defined as the ratio of current value and moving average of a certain window size:\n",
"$$\\begin{aligned}\n",
"\\Delta(s, r, t) = \\left\\{\\begin{aligned}\n",
"&\\frac{TX(s, r, t)}{\\sum_{\\tau=t-T}^{t-1}X(\\tau)}, &t\\geq T\\\\\n",
"&1, &0<t<T\n",
"\\end{aligned}\\right.\n",
"\\end{aligned}$$\n",
"Where $X(s, r, t)$ is the carbon emission value of sector $s$, region $r$ and date $t$; $T$ is the window size with default value $T=14$."
]
},
{
"cell_type": "code",
2022-01-25 08:34:45 +03:00
"execution_count": 5,
2021-12-31 12:46:06 +03:00
"metadata": {},
"outputs": [],
"source": [
"def calculate_rate_of_change(df, window_size=14):\n",
" region_scope = 'state' if 'state' in df.columns else 'country_region'\n",
" new_df = pd.DataFrame()\n",
" for sector in set(df['sector']):\n",
" sector_mask = df['sector'] == sector\n",
" for region, values in df.loc[sector_mask].pivot(index='date', columns=region_scope, values='value').items():\n",
" values.fillna(0)\n",
" rates = values / values.rolling(window_size).mean()\n",
" rates.fillna(value=1, inplace=True)\n",
" tmp_df = pd.DataFrame(\n",
" index=values.index,\n",
" columns=['value', 'rate_of_change'],\n",
" data=np.array([values.to_numpy(), rates.to_numpy()]).T\n",
" )\n",
" tmp_df['sector'] = sector\n",
" tmp_df[region_scope] = region\n",
" new_df = new_df.append(tmp_df.reset_index())\n",
" return new_df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <a id=\"a13\"></a> 1.3. Expand country regions\n",
"*Note: This step applies only to the ```carbon_global``` dataset.*\n",
"\n",
"The dataset ```carbon_global``` does not list all the countries/regions in the world. Instead, there are two groups which contains multiple countries/regions: ```ROW``` (i.e. the rest of the world) and ```EU27 & UK```.\n",
"\n",
"In order to obtain the carbon emission data of countries/regions in these two groups, we can refer to [the EDGAR dataset](https://edgar.jrc.ec.europa.eu/dataset_ghg60) and use [the table of CO2 emissions of all world countries in 2019](./static_data/Baseline.csv) as the baseline.\n",
"\n",
"Assume the the carbon emission of each non-listed country/region is linearly related to the carbon emission of the group it belongs to, we have:\n",
"$$\\begin{aligned}\n",
"X(s, r, t) &= \\frac{\\sum_{r_i\\in R(r)}X(s, r_i, t)}{\\sum_{r_i\\in R(r)}X(s, r_i, t_0)}X(s, r, t_0)\\\\\n",
"&= \\frac{X_{Raw}(s, R(r), t)}{\\sum_{r_i\\in R(r)}X_{Baseline}(s, r_i)}X_{Baseline}(s, r)\n",
"\\end{aligned}$$\n",
"Where\n",
"- $X(s, r, t)$ is the carbon emission value of sector $s$, country/region $r$ and date $t$.\n",
"- $t_0$ is the date of the baseline table.\n",
"- $R(r)$ is the group that contains country/region $r$.\n",
"- $X_{Raw}(s, R, t)$ is the carbon emission value of sector $s$, country/region group $R$ and date $t$ in the ```carbon_global``` dataset.\n",
"- $X_{Baseline}(s, r)$ is the carbon emission value of sector $s$ and country/region $r$ in the baseline table.\n",
"\n",
"Note that the baseline table does not contain the ```International Aviation``` sector. Therefore, the data for ```International Aviation``` is only available to countries listed in the ```carbon_global``` dataset. When we expand the ```ROW``` and the ```EU27 & UK``` groups to other countries/regions of the world, only the other five sectors are considered."
]
},
{
"cell_type": "code",
2022-01-25 08:34:45 +03:00
"execution_count": 6,
2021-12-31 12:46:06 +03:00
"metadata": {},
"outputs": [],
"source": [
"def expand_country_regions(df):\n",
"\n",
" sectors = set(df['sector'])\n",
" assert 'country_region' in df.columns\n",
" df = df.replace('US', 'United States')\n",
" df = df.replace('UK', 'United Kingdom')\n",
" original_country_regions = set(df['country_region'])\n",
"\n",
" country_region_df = pd.read_csv('static_data/CountryRegion.csv')\n",
" base = {}\n",
" name_to_code = {}\n",
" for _, (name, code, source) in country_region_df.loc[:, ['Name', 'Code', 'DataSource']].iterrows():\n",
" if source.startswith('Simulated') and name not in original_country_regions:\n",
" name_to_code[name] = code\n",
" base[code] = 'ROW' if source.endswith('ROW') else 'EU27 & UK'\n",
"\n",
" baseline = pd.read_csv('static_data/Baseline.csv')\n",
" baseline = baseline.set_index('CountryRegionCode')\n",
" baseline = baseline.loc[:, [sector for sector in baseline.columns if sector in sectors]]\n",
" group_baseline = {}\n",
" for group in original_country_regions & set(['ROW', 'EU27 & UK']):\n",
" group_baseline[group] = baseline.loc[[code for code, base_group in base.items() if base_group == group], :].sum()\n",
"\n",
" new_df = pd.DataFrame()\n",
" sector_masks = {sector: df['sector'] == sector for sector in sectors}\n",
" for country_region in set(country_region_df['Name']):\n",
" if country_region in name_to_code:\n",
" code = name_to_code[country_region]\n",
" group = base[code]\n",
" group_mask = df['country_region'] == group\n",
" for sector, sum_value in group_baseline[group].items():\n",
" tmp_df = df.loc[sector_masks[sector] & group_mask, :].copy()\n",
" tmp_df['value'] = tmp_df['value'] / sum_value * baseline.loc[code, sector]\n",
" tmp_df['country_region'] = country_region\n",
" new_df = new_df.append(tmp_df)\n",
" elif country_region in original_country_regions:\n",
" new_df = new_df.append(df.loc[df['country_region'] == country_region])\n",
"\n",
" return new_df\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. <a id=\"a2\"></a> Visualize Data\n",
"This is a auxiliary module for displaying data, which can be modified arbitrarily."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <a id=\"a21\"></a> 2.1. Plot by date\n",
"In this part we are going to create a line chart, where the emission value and rate of change for given counties during the given time can be browsed."
]
},
{
"cell_type": "code",
2022-01-25 08:34:45 +03:00
"execution_count": 7,
2021-12-31 12:46:06 +03:00
"metadata": {},
"outputs": [],
"source": [
"def plot_by_date(df, start_date=None, end_date=None, sector=None, regions=None, title='Carbon Emission by Date'):\n",
" if start_date is None:\n",
" start_date = df['date'].min()\n",
" if end_date is None:\n",
" end_date = df['date'].max()\n",
" tmp_df = df.loc[(df['date'] >= start_date) & (df['date'] <= end_date)]\n",
"\n",
" region_scope = 'state' if 'state' in tmp_df.columns else 'country_region'\n",
" if regions is None or type(regions) == int:\n",
" region_list = list(set(tmp_df[region_scope]))\n",
" sector_mask = True if sector is None else tmp_df['sector'] == sector\n",
" region_list.sort(key=lambda region: -tmp_df.loc[(tmp_df[region_scope] == region) & sector_mask, 'value'].sum())\n",
" regions = region_list[:3 if regions is None else regions]\n",
" tmp_df = pd.concat([tmp_df.loc[tmp_df[region_scope] == region] for region in regions])\n",
"\n",
" if sector not in set(tmp_df['sector']):\n",
" tmp_df['rate_of_change'] = tmp_df['value'] / tmp_df['rate_of_change']\n",
" tmp_df = tmp_df.groupby(['date', region_scope]).sum().reset_index()\n",
" value_df = tmp_df.pivot(index='date', columns=region_scope, values='value')\n",
" rate_df = tmp_df.pivot(index='date', columns=region_scope, values='rate_of_change')\n",
" rate_df = value_df / rate_df\n",
" else:\n",
" tmp_df = tmp_df.loc[tmp_df['sector'] == sector, [region_scope, 'date', 'value', 'rate_of_change']]\n",
" value_df = tmp_df.pivot(index='date', columns=region_scope, values='value')\n",
" rate_df = tmp_df.pivot(index='date', columns=region_scope, values='rate_of_change')\n",
" value_df = value_df.loc[:, regions]\n",
" rate_df = rate_df.loc[:, regions]\n",
"\n",
" fig = plt.figure(figsize=(10, 8))\n",
" fig.suptitle(title)\n",
"\n",
" plt.subplot(2, 1, 1)\n",
" plt.plot(value_df)\n",
" plt.ylabel('Carbon Emission Value / Mt CO2')\n",
" plt.xticks(rotation=60)\n",
" plt.legend(regions, loc='upper right')\n",
"\n",
" plt.subplot(2, 1, 2)\n",
" plt.plot(rate_df)\n",
" plt.ylabel('Rate of Change')\n",
" plt.xticks(rotation=60)\n",
" plt.legend(regions, loc='upper right')\n",
"\n",
" plt.subplots_adjust(hspace=0.3)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <a id=\"a22\"></a> 2.2. Plot by sector\n",
"Generally, sources of emissions can be divided into five or six categories:\n",
"- Domestic Aviation\n",
"- Ground Transport\n",
"- Industry\n",
"- Power \n",
"- Residential\n",
"- International Aviation\n",
"\n",
"Where the data of ```International Aviation``` are only available to ```carbon_global``` and ```carbon_us``` datasets. For ```carbon_global``` dataset, we can not expand the data for International Aviation of non-listed countries.\n",
"\n",
"Let's create a pie chart and a stacked column chart, where you can focus on details of specific countiy/regions emission data, including quantity and percentage breakdown by above sectors. "
]
},
{
"cell_type": "code",
2022-01-25 08:34:45 +03:00
"execution_count": 8,
2021-12-31 12:46:06 +03:00
"metadata": {},
"outputs": [],
"source": [
"def plot_by_sector(df, start_date=None, end_date=None, sectors=None, region=None, title='Carbon Emission Data by Sector'):\n",
" if start_date is None:\n",
" start_date = df['date'].min()\n",
" if end_date is None:\n",
" end_date = df['date'].max()\n",
" tmp_df = df.loc[(df['date'] >= start_date) & (df['date'] <= end_date)]\n",
"\n",
" region_scope = 'state' if 'state' in df.columns else 'country_region'\n",
" if region in set(tmp_df[region_scope]):\n",
" tmp_df = tmp_df.loc[tmp_df[region_scope] == region]\n",
"\n",
" if sectors is None:\n",
" sectors = list(set(tmp_df['sector']))\n",
" sectors.sort(key=lambda sector: -tmp_df.loc[tmp_df['sector'] == sector, 'value'].sum())\n",
" tmp_df = tmp_df.loc[[sector in sectors for sector in tmp_df['sector']]]\n",
"\n",
" fig = plt.figure(figsize=(10, 8))\n",
" fig.suptitle(title)\n",
"\n",
" plt.subplot(2, 1, 1)\n",
" data = np.array([tmp_df.loc[tmp_df['sector'] == sector, 'value'].sum() for sector in sectors])\n",
" total = tmp_df['value'].sum()\n",
" bbox_props = dict(boxstyle=\"square,pad=0.3\", fc=\"w\", ec=\"k\", lw=0.72)\n",
" kw = dict(arrowprops=dict(arrowstyle=\"-\"),\n",
" bbox=bbox_props, zorder=0, va=\"center\")\n",
" wedges, texts = plt.pie(data, wedgeprops=dict(width=0.5), startangle=90)\n",
" for i, p in enumerate(wedges):\n",
" factor = data[i] / total * 100\n",
" if factor > 5:\n",
" ang = (p.theta2 - p.theta1)/2. + p.theta1\n",
" y = np.sin(np.deg2rad(ang))\n",
" x = np.cos(np.deg2rad(ang))\n",
" horizontalalignment = {-1: \"right\", 1: \"left\"}[int(np.sign(x))]\n",
" connectionstyle = \"angle,angleA=0,angleB={}\".format(ang)\n",
" kw[\"arrowprops\"].update({\"connectionstyle\": connectionstyle})\n",
" text = '{}\\n{:.1f} Mt CO2 ({:.1f}%)'.format(sectors[i], data[i], factor)\n",
" plt.annotate(\n",
" text,\n",
" xy=(x, y),\n",
" xytext=(1.35 * np.sign(x), 1.4 * y),\n",
" horizontalalignment=horizontalalignment,\n",
" **kw\n",
" )\n",
" plt.axis('equal')\n",
"\n",
" plt.subplot(2, 1, 2)\n",
" labels = []\n",
" data = [[] for _ in sectors]\n",
" date = pd.to_datetime(start_date)\n",
" delta = pd.DateOffset(months=1)\n",
" while date <= pd.to_datetime(end_date):\n",
" sub_df = tmp_df.loc[(tmp_df['date'] >= date) & (tmp_df['date'] < date + delta)]\n",
" for i, sector in enumerate(sectors):\n",
" data[i].append(sub_df.loc[sub_df['sector'] == sector, 'value'].sum())\n",
" labels.append(date.strftime('%Y-%m'))\n",
" date += delta\n",
" data = np.array(data)\n",
" for i, sector in enumerate(sectors):\n",
" plt.bar(labels, data[i], bottom=data[:i].sum(axis=0), label=sector)\n",
" plt.xticks(rotation=60)\n",
" plt.legend()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## <a id=\"a3\"></a> 3. Examples"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <a id=\"a31\"></a> 3.1. World carbon emission data"
]
},
{
"cell_type": "code",
2022-01-25 08:34:45 +03:00
"execution_count": 9,
2021-12-31 12:46:06 +03:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Download carbon_global data\n",
"Calculate rate of change\n",
2022-01-04 10:58:46 +03:00
"Expand country / regions\n",
"Export Data to export_data\\global_carbon_emission_data.csv\n"
2021-12-31 12:46:06 +03:00
]
},
{
"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>value</th>\n",
" <th>rate_of_change</th>\n",
" <th>sector</th>\n",
" <th>country_region</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>2019-01-01</td>\n",
2022-02-07 09:54:49 +03:00
" <td>0.112941</td>\n",
2021-12-31 12:46:06 +03:00
" <td>1.000000</td>\n",
" <td>Power</td>\n",
2022-02-07 09:54:49 +03:00
" <td>Montenegro</td>\n",
2021-12-31 12:46:06 +03:00
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2019-01-02</td>\n",
2022-02-07 09:54:49 +03:00
" <td>0.120552</td>\n",
2021-12-31 12:46:06 +03:00
" <td>1.000000</td>\n",
" <td>Power</td>\n",
2022-02-07 09:54:49 +03:00
" <td>Montenegro</td>\n",
2021-12-31 12:46:06 +03:00
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2019-01-03</td>\n",
2022-02-07 09:54:49 +03:00
" <td>0.126057</td>\n",
2021-12-31 12:46:06 +03:00
" <td>1.000000</td>\n",
" <td>Power</td>\n",
2022-02-07 09:54:49 +03:00
" <td>Montenegro</td>\n",
2021-12-31 12:46:06 +03:00
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2019-01-04</td>\n",
2022-02-07 09:54:49 +03:00
" <td>0.132794</td>\n",
2021-12-31 12:46:06 +03:00
" <td>1.000000</td>\n",
" <td>Power</td>\n",
2022-02-07 09:54:49 +03:00
" <td>Montenegro</td>\n",
2021-12-31 12:46:06 +03:00
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2019-01-05</td>\n",
2022-02-07 09:54:49 +03:00
" <td>0.129115</td>\n",
2021-12-31 12:46:06 +03:00
" <td>1.000000</td>\n",
" <td>Power</td>\n",
2022-02-07 09:54:49 +03:00
" <td>Montenegro</td>\n",
2021-12-31 12:46:06 +03:00
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
2022-02-07 09:54:49 +03:00
" <th>1091</th>\n",
" <td>2021-12-27</td>\n",
" <td>0.010651</td>\n",
" <td>0.970426</td>\n",
2021-12-31 12:46:06 +03:00
" <td>Residential</td>\n",
2022-02-07 09:54:49 +03:00
" <td>Estonia</td>\n",
2021-12-31 12:46:06 +03:00
" </tr>\n",
" <tr>\n",
2022-02-07 09:54:49 +03:00
" <th>1092</th>\n",
" <td>2021-12-28</td>\n",
" <td>0.009503</td>\n",
" <td>0.868008</td>\n",
2021-12-31 12:46:06 +03:00
" <td>Residential</td>\n",
2022-02-07 09:54:49 +03:00
" <td>Estonia</td>\n",
2021-12-31 12:46:06 +03:00
" </tr>\n",
" <tr>\n",
2022-02-07 09:54:49 +03:00
" <th>1093</th>\n",
" <td>2021-12-29</td>\n",
" <td>0.008750</td>\n",
" <td>0.802686</td>\n",
2021-12-31 12:46:06 +03:00
" <td>Residential</td>\n",
2022-02-07 09:54:49 +03:00
" <td>Estonia</td>\n",
2021-12-31 12:46:06 +03:00
" </tr>\n",
" <tr>\n",
2022-02-07 09:54:49 +03:00
" <th>1094</th>\n",
" <td>2021-12-30</td>\n",
" <td>0.007343</td>\n",
" <td>0.683108</td>\n",
2021-12-31 12:46:06 +03:00
" <td>Residential</td>\n",
2022-02-07 09:54:49 +03:00
" <td>Estonia</td>\n",
2021-12-31 12:46:06 +03:00
" </tr>\n",
" <tr>\n",
2022-02-07 09:54:49 +03:00
" <th>1095</th>\n",
" <td>2021-12-31</td>\n",
" <td>0.007100</td>\n",
" <td>0.673975</td>\n",
2021-12-31 12:46:06 +03:00
" <td>Residential</td>\n",
2022-02-07 09:54:49 +03:00
" <td>Estonia</td>\n",
2021-12-31 12:46:06 +03:00
" </tr>\n",
" </tbody>\n",
"</table>\n",
2022-02-07 09:54:49 +03:00
"<p>1047776 rows × 5 columns</p>\n",
2021-12-31 12:46:06 +03:00
"</div>"
],
"text/plain": [
2022-02-07 09:54:49 +03:00
" date value rate_of_change sector country_region\n",
"0 2019-01-01 0.112941 1.000000 Power Montenegro\n",
"1 2019-01-02 0.120552 1.000000 Power Montenegro\n",
"2 2019-01-03 0.126057 1.000000 Power Montenegro\n",
"3 2019-01-04 0.132794 1.000000 Power Montenegro\n",
"4 2019-01-05 0.129115 1.000000 Power Montenegro\n",
"... ... ... ... ... ...\n",
"1091 2021-12-27 0.010651 0.970426 Residential Estonia\n",
"1092 2021-12-28 0.009503 0.868008 Residential Estonia\n",
"1093 2021-12-29 0.008750 0.802686 Residential Estonia\n",
"1094 2021-12-30 0.007343 0.683108 Residential Estonia\n",
"1095 2021-12-31 0.007100 0.673975 Residential Estonia\n",
2021-12-31 12:46:06 +03:00
"\n",
2022-02-07 09:54:49 +03:00
"[1047776 rows x 5 columns]"
2021-12-31 12:46:06 +03:00
]
},
2022-01-25 08:34:45 +03:00
"execution_count": 9,
2021-12-31 12:46:06 +03:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data_type = 'carbon_global'\n",
"print(f'Download {data_type} data')\n",
"global_df = get_data_from_carbon_monitor(data_type)\n",
"print('Calculate rate of change')\n",
"global_df = calculate_rate_of_change(global_df)\n",
"print('Expand country / regions')\n",
"global_df = expand_country_regions(global_df)\n",
2022-01-04 10:58:46 +03:00
"\n",
"export_data('global_carbon_emission_data.csv', global_df)\n",
2021-12-31 12:46:06 +03:00
"global_df"
]
},
{
"cell_type": "code",
2022-01-25 08:34:45 +03:00
"execution_count": 10,
2021-12-31 12:46:06 +03:00
"metadata": {},
"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Y1WjbbPdg/cs+C4iqM87xXjsLNp2Jwf/PeyTUtYWPaK29e221/ES78u1/TaUiNgphHhcWMH+30dZi/4thNgmhPh8mf2LP+dOY5lJtyF6TEb6ew/5e9k4kO+w7LZCiM8KITYINWGjD2W9a6Q0twFvFUJ4URbfF6WUxb81lP9uhyODEkJ23CjBMaLPMwwzgM8U9QPTsP63e4sEd6nPBoAh4m6QUr4GqEUNjH4rhDiu1PYjvM6Kz3VK0bl+EWWdBWVdnQdsFEK8IIS4aLgPX3Q+b0YJyfOllF3G4ghQXbRpNco9OxIuA2ah+oZfoEIh9hjHu1FYE2u+WHQu30dZzP7L9v2P6FyEEFehYsMuNMRtJT6H5hCiRdhRgJTycVQsjDnC242KTbDfyINSyu8Y2/9LSnke6ua3ETXiLGY/qsMG8ibwabb1u1EuGPsx/FLKp0dyysOs/wzKOnGK4X4wXS2i/C55HmJot8q7gUtQbssaYGaJtkudn/27CKFcOGZ81Iyibaej4juOGKSUL0gpL0G5hv6O4c4xLBWfkVLOBt6EcjmdW6KJ4s853Vg2Wh4GWoUQyyvQ1pAIIc4A/gfl0quTyhXeT5n/lZRyPUqYnE95V2TZ73YE7ML6/5nMYggxdIDsBr5VdI0GDGvtfmCq3a2I+k2HRUoZl1LegIrXXGAuLtpsuOusePvdwPaic62SUl5gHHOzlPJdqO/4u6jA8yAjQAjxRlQfd7GUco1t1SbAJYQ4xrZsCZabb0iklDullBdJKZuklKegBObzxrorpTWx5tu2c/ka6v/0eiml3Qq3Dlhc9Hsstp+LYcn7PHCulHJP0b6zDWvtAX8OzaFFi7Cjh+uB84QQS1AjsouFEG8wRqQ+oaa+twohWoQQlxgdWBI1isqVaO8
2021-12-31 12:46:06 +03:00
"text/plain": [
"<Figure size 720x576 with 2 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"plot_by_date(\n",
" global_df,\n",
" start_date='2019-01-01',\n",
" end_date='2020-12-31',\n",
" sector='Residential',\n",
" regions=['China', 'United States'],\n",
" title='Residential Carbon Emission, China v.s. United States, 2019-2020'\n",
")"
]
},
{
"cell_type": "code",
2022-01-25 08:34:45 +03:00
"execution_count": 11,
2021-12-31 12:46:06 +03:00
"metadata": {},
"outputs": [
{
"data": {
2022-02-07 09:54:49 +03:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAlwAAAIrCAYAAAA6O0fJAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAACoL0lEQVR4nOzdd3gVxdvG8e8QQhMI3ZdqkJpK6CBFihSNYkFARcHesIMaCxoRFSvY0J8dFUFFEDQ2EJAuzUhoIiXSkRoIoYXM+8eeHBOSQAgJe05yf65rL3J2Z2efPQk5T2ZmZ4y1FhEREREpOMXcDkBERESksFPCJSIiIlLAlHCJiIiIFDAlXCIiIiIFTAmXiIiISAFTwiUiIiJSwJRwiQDGmFhjzOcnOZ5ojLnoLMYz0xhz69m6Xn4wxtQxxiQbYwLOoI5kY8z5+RmXp96z+v0TETmREi7xC8aYx4wxP56w7+8c9l1zdqMDY0wrY8wPxph9xpg9xpiFxpibznYcp8sYc6Mx5rgn0cm41Tjduqy1G621Za21x/Maj+f89Xk9/2wwxjxujNngeZ82G2O+zIc6XUkIjTEljTEfGmP+McYcMMbEG2MuPqFMV2PMamNMijFmhjHmvAzH+hpj5nmOzcym/suMMcs979U8Y0zoSWJpaIyZbIzZ6fk/9LMxptEJZR40xmw3xuw3xnxkjCnp2V/NGDPOGLPVGJNkjJlrjGl9wrnXee7zoDHmW2NMpTy+bSJ5ooRL/MUs4IL01hNjTHUgEGh6wr76nrK5ZowpfiaBGWPaAtOB3zzXrwzcBVx8svNyqMsYY872/8v5nkQn47b1LMfgF4wxA4EbgIustWWBFsCvLsd0Jj+/xYFNwIVAEPAk8JUxJthTdxVgIjAUqAQsBjImmHuAUcCIbOJqAIw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WMp+wWfIi19mdrbTecZHQ18L5n/3Zr7W3W2hrAHcBoY0z9E07fxX8tYelO/B6erhPfz4wPBozB6Vbsg9P6ecrrWGuPWWu/xhnzFI6TAB0BqmRIXstba9O73zbhdP1lW90Jr3PzPc50jrX2C2tte895FnjxVPeQoe70sXm9M7SwgtO12CRD2XM897CCXLDWTrDWhltrKwNP44zzW+Q5lvEhjY2e+iviJFtTrLXPnVBdplg8X+9I73L0PLH4LU5Cd8fJzjXO1CMlgTW5uQ+R/KCES/yGp+tkMU43w+wMh+Z49s3ylNuE0/L1gjGmlDEmErgFp+soNyYAlxpj2nu6QoZx8v8rjwA3esb1VAYwxjQxxoz3HC+H0xK0z9MS93Qu47jeGBPqSfiGARM83WdfAdHGeVw/ECehO+K5Z59gjDnXGHO55wP6CE4LQ5rnWB9PlxE4LYSWE7psM9znc8aYcsaZiuAhcv89zM4gY0wtz/fgCTI/bfctzgD2+3HGdOV0XzcaY6I9MRUzzhQKYTjdrNtwkoVXjTHlPcfrGWPSu48/AIYYY5p7nkatb/6bYmEHzli1dKf1PTbGNDLGdPEkHYdxft7S3+9OxpiT/YHxDhCCM4bsxO71STjdwr2NMaWAp4Bl6V2OxpgAz/7iQDHP/7fADHE195SpCryHk0itJhuersCfgbnW2phsinwK3OL5P1EBZwzeJ55zA3H+3x4CBnpaBDMaC1xmjOng+ZkcBkw8oRVcpEAp4RJ/8xtOF03GcT+zPfsyTgdxLc5f01txPjSettZOy80FPGONBuE8FbUNJynYfJLy84Aunm29MWYPzofLD54io3DG3+zCGXT9U27iwOni+QSn66wUnsHp1tq/cFpj3vTUeRnOh+XRXNZ7orYm6zxcLfNYV7piOAnSVpwnRS/EmSoDoCXwuzEmGedJ0vtt9nNv3YvzZOR6nO/3F8BHZxDTFzgJ0Xqcrr3h6Qc8icY
2021-12-31 12:46:06 +03:00
"text/plain": [
"<Figure size 720x576 with 2 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"plot_by_sector(\n",
" global_df,\n",
" start_date='2019-01-01',\n",
" end_date='2020-12-31',\n",
" sectors=None,\n",
" region=None,\n",
" title='World Carbon Emission by Sectors, 2019-2020',\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <a id=\"a32\"></a> 3.2. US carbon emission data"
]
},
{
"cell_type": "code",
2022-01-25 08:34:45 +03:00
"execution_count": 12,
2021-12-31 12:46:06 +03:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Download carbon_us data\n",
2022-01-04 10:58:46 +03:00
"Calculate rate of change\n",
"Export Data to export_data\\us_carbon_emission_data.csv\n"
2021-12-31 12:46:06 +03:00
]
},
{
"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>value</th>\n",
" <th>rate_of_change</th>\n",
" <th>sector</th>\n",
" <th>state</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>2019-01-01</td>\n",
2022-02-07 09:54:49 +03:00
" <td>9.917140e-02</td>\n",
2021-12-31 12:46:06 +03:00
" <td>1.000000</td>\n",
2022-02-07 09:54:49 +03:00
" <td>Power</td>\n",
2021-12-31 12:46:06 +03:00
" <td>Alabama</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2019-01-02</td>\n",
2022-02-07 09:54:49 +03:00
" <td>1.142040e-01</td>\n",
2021-12-31 12:46:06 +03:00
" <td>1.000000</td>\n",
2022-02-07 09:54:49 +03:00
" <td>Power</td>\n",
2021-12-31 12:46:06 +03:00
" <td>Alabama</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2019-01-03</td>\n",
2022-02-07 09:54:49 +03:00
" <td>1.180630e-01</td>\n",
2021-12-31 12:46:06 +03:00
" <td>1.000000</td>\n",
2022-02-07 09:54:49 +03:00
" <td>Power</td>\n",
2021-12-31 12:46:06 +03:00
" <td>Alabama</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2019-01-04</td>\n",
2022-02-07 09:54:49 +03:00
" <td>1.091590e-01</td>\n",
2021-12-31 12:46:06 +03:00
" <td>1.000000</td>\n",
2022-02-07 09:54:49 +03:00
" <td>Power</td>\n",
2021-12-31 12:46:06 +03:00
" <td>Alabama</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2019-01-05</td>\n",
2022-02-07 09:54:49 +03:00
" <td>1.003130e-01</td>\n",
2021-12-31 12:46:06 +03:00
" <td>1.000000</td>\n",
2022-02-07 09:54:49 +03:00
" <td>Power</td>\n",
2021-12-31 12:46:06 +03:00
" <td>Alabama</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
2022-02-07 09:54:49 +03:00
" <th>1091</th>\n",
" <td>2021-12-27</td>\n",
" <td>2.490000e-05</td>\n",
" <td>2.234615</td>\n",
2021-12-31 12:46:06 +03:00
" <td>International Aviation</td>\n",
" <td>Wyoming</td>\n",
" </tr>\n",
" <tr>\n",
2022-02-07 09:54:49 +03:00
" <th>1092</th>\n",
" <td>2021-12-28</td>\n",
" <td>0.000000e+00</td>\n",
2022-01-25 08:34:45 +03:00
" <td>0.000000</td>\n",
2021-12-31 12:46:06 +03:00
" <td>International Aviation</td>\n",
" <td>Wyoming</td>\n",
" </tr>\n",
" <tr>\n",
2022-02-07 09:54:49 +03:00
" <th>1093</th>\n",
" <td>2021-12-29</td>\n",
" <td>5.000000e-07</td>\n",
" <td>0.053805</td>\n",
2021-12-31 12:46:06 +03:00
" <td>International Aviation</td>\n",
" <td>Wyoming</td>\n",
" </tr>\n",
" <tr>\n",
2022-02-07 09:54:49 +03:00
" <th>1094</th>\n",
" <td>2021-12-30</td>\n",
" <td>1.750000e-05</td>\n",
" <td>1.670075</td>\n",
2021-12-31 12:46:06 +03:00
" <td>International Aviation</td>\n",
" <td>Wyoming</td>\n",
" </tr>\n",
" <tr>\n",
2022-02-07 09:54:49 +03:00
" <th>1095</th>\n",
" <td>2021-12-31</td>\n",
" <td>1.750000e-05</td>\n",
" <td>1.772793</td>\n",
2021-12-31 12:46:06 +03:00
" <td>International Aviation</td>\n",
" <td>Wyoming</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
2022-02-07 09:54:49 +03:00
"<p>335376 rows × 5 columns</p>\n",
2021-12-31 12:46:06 +03:00
"</div>"
],
"text/plain": [
2022-02-07 09:54:49 +03:00
" date value rate_of_change sector state\n",
"0 2019-01-01 9.917140e-02 1.000000 Power Alabama\n",
"1 2019-01-02 1.142040e-01 1.000000 Power Alabama\n",
"2 2019-01-03 1.180630e-01 1.000000 Power Alabama\n",
"3 2019-01-04 1.091590e-01 1.000000 Power Alabama\n",
"4 2019-01-05 1.003130e-01 1.000000 Power Alabama\n",
"... ... ... ... ... ...\n",
"1091 2021-12-27 2.490000e-05 2.234615 International Aviation Wyoming\n",
"1092 2021-12-28 0.000000e+00 0.000000 International Aviation Wyoming\n",
"1093 2021-12-29 5.000000e-07 0.053805 International Aviation Wyoming\n",
"1094 2021-12-30 1.750000e-05 1.670075 International Aviation Wyoming\n",
"1095 2021-12-31 1.750000e-05 1.772793 International Aviation Wyoming\n",
2021-12-31 12:46:06 +03:00
"\n",
2022-02-07 09:54:49 +03:00
"[335376 rows x 5 columns]"
2021-12-31 12:46:06 +03:00
]
},
2022-01-25 08:34:45 +03:00
"execution_count": 12,
2021-12-31 12:46:06 +03:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data_type = 'carbon_us'\n",
"print(f'Download {data_type} data')\n",
"us_df = get_data_from_carbon_monitor(data_type)\n",
"print('Calculate rate of change')\n",
"us_df = calculate_rate_of_change(us_df)\n",
2022-01-04 10:58:46 +03:00
"export_data('us_carbon_emission_data.csv', us_df)\n",
2021-12-31 12:46:06 +03:00
"us_df"
]
},
{
"cell_type": "code",
2022-01-25 08:34:45 +03:00
"execution_count": 13,
2021-12-31 12:46:06 +03:00
"metadata": {},
"outputs": [
{
"data": {
2022-02-07 09:54:49 +03:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAmcAAAIqCAYAAABhdO2jAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAAEAAElEQVR4nOyddZgcRfrHvzW+s27Z6MbdhYRAgIQEd3f7IYe7c7jfccBx2AWHwyUQnCQkhEAc4glx2SSbdR2fqd8f1dVd3dMzO+uTTX2eZ56ZaZualqq3XiWUUkgkEolEIpFIkgNLezdAIpFIJBKJRKIhhTOJRCKRSCSSJEIKZxKJRCKRSCRJhBTOJBKJRCKRSJIIKZxJJBKJRCKRJBFSOJNIJBKJRCJJIqRwJpG0M4QQSgjp197taAyEkAsIIT81Y//DCCF/tWSbJBKJpKMghTNJh8ZM8CGEPEQI+Z/w/V5CyDZCSB0hpIgQ8nEDxzyfELJM2X4vIeR7Qsik1voPLQUh5G1CSEBpN3+tbMqxKKXvU0qPbmpbKKW/UkoHNnX/RFCuK/+fPkJIWPi+toV+Y4hyL1Qqr9mEkCFxth9KCPmJEFJBCKkihCwnhByvrJtMCClq5O+3umBPCHESQt4ghOwghNQSQlYQQo4zbDOVELKBEOIhhMwlhPQU1p1NCPldWTfP5PgnEULWKNfl9wbO3wBCyFeEkFLlHP5ICBlo2OYWQkgxIaSGEPImIcSpLO9ECPmQELKHEFJNCPmNEDLBsO/5y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k3lHqgCM782dD9dDWYOrAJwtrLsWOVcVynrPgUT8kYo/6kWLL3FN2DmToClj/gtRru5ls1nuHcuELaZprTTq5y/XoZzTw2vt4X1C4Q2/RdAapz
2021-12-31 12:46:06 +03:00
"text/plain": [
"<Figure size 720x576 with 2 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"plot_by_date(\n",
" us_df,\n",
" start_date='2019-01-01',\n",
" end_date='2020-12-31',\n",
" sector=None,\n",
" regions=3,\n",
" title='US Carbon Emission, Top 3 States, 2019-2020'\n",
")\n"
]
},
{
"cell_type": "code",
2022-01-25 08:34:45 +03:00
"execution_count": 14,
2021-12-31 12:46:06 +03:00
"metadata": {},
"outputs": [
{
"data": {
2022-02-07 09:54:49 +03:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAlAAAAIrCAYAAAAgB6dHAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAACreklEQVR4nOzdd3hURdvA4d+kUEKAUKWGQCipJBB6kyI1ioA0RekgCnbRvPqiEUEDiqKAoH5KkSoowksURYo0kRoIEKTGEECkQwgtYb4/zrImpJfNySbPfV3nyu4pc56zm2SfnZkzo7TWCCGEEEKIzHMwOwAhhBBCCHsjCZQQQgghRBZJAiWEEEIIkUWSQAkhhBBCZJEkUEIIIYQQWSQJlBBCCCFEFkkCJfINpZRWStW2PJ6llBqXZNszSqmzSqk4pVS5XD7vAKXUL7lZZk4opTYopYabHUdWKKXcLe+NYw7KiFNK1crNuCzlRiulHsrtcoUQhZskUCJXKaWeUErttHwYnlFK/aSUapXVcrTWo7TW71rKdAY+AjpprV211hdyM2at9QKtdafsHq+UaqKU+lEpdVkpdVEptV0pNSQ3Y7QFpdRgpVSi5b1KulTJalla6xjLe5OY3Xgsxx/P7vF5QSn1hlLqhOV1ilVKLcm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SoAX2AkRodIhaXp7Wdgi9Y6JJVd5gHDLH8Tbhh92OZYjnXG6Id4AxhkqbFLagHwiFKqteV3cjzwvaVJXYhskwRK5BpLf6GXMf65ncP41j0G4wMKjLvDnsCoOv+S5B9iWTnPBYwO4a9gNB2+BjystT6fg/Dv+Rl
2021-12-31 12:46:06 +03:00
"text/plain": [
"<Figure size 720x576 with 2 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"plot_by_sector(\n",
" us_df,\n",
" start_date='2019-01-01',\n",
" end_date='2020-12-31',\n",
" sectors = None,\n",
" region='California',\n",
" title='California Carbon Emission by Sectors, 2019-2020',\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# B. Co-Analysis of Carbon Emission Data v.s. COVID-19 Data\n",
"\n",
"This section will help you to visualize the relativity between carbon emissions in different countries and the trends of the COVID-19 pandemic since January 2020 provided by [Oxford COVID-19 Government Response Tracker](https://covidtracker.bsg.ox.ac.uk/). The severity of the epidemic can be shown in three aspects: the number of new diagnoses, the number of deaths and the stringency and policy indices of governments.\n",
"\n",
"Overview:\n",
"- [Download data from Oxford COVID-19 Government Response Tracker](#b1)\n",
"- [Visualize COVID-19 data and carbon emission data](#b2)\n",
"- [Example: COVID-19 cases and stringency index v.s. carbon emission in US](#b3)"
]
},
{
"cell_type": "code",
2022-01-25 08:34:45 +03:00
"execution_count": 15,
2021-12-31 12:46:06 +03:00
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"import datetime\n",
"from urllib.request import urlopen"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. <a id=\"b1\"></a> Download COVID-19 Data\n",
"\n",
"We are going to download JSON-formatted COVID-19 data and convert to Pandas Dataframe. The Oxford COVID-19 Government Response Tracker dataset provides confirmed cases, deaths and stringency index data for all countries/regions since January 2020.\n",
"\n",
"- The ```confirmed``` measurement records the total number of confirmed COVID-19 cases since January 2020. We will convert it into incremental data.\n",
"- The ```deaths``` measurement records the total number of patients who died due to infection with COVID-19 since January 2020. We will convert it into incremental data.\n",
"- The ```stringency``` measurement means the Stringency Index, which is a float number from 0 to 100 that reflects how strict a countrys measures were, including lockdown, school closures, travel bans, etc. A higher score indicates a stricter response (i.e. 100 = strictest response). \n"
]
},
{
"cell_type": "code",
2022-01-25 08:34:45 +03:00
"execution_count": 16,
2021-12-31 12:46:06 +03:00
"metadata": {},
"outputs": [],
"source": [
"def get_covid_data_from_oxford_covid_tracker(data_type='carbon_global'):\n",
" data = json.loads(urlopen(\"https://covidtrackerapi.bsg.ox.ac.uk/api/v2/stringency/date-range/{}/{}\".format(\n",
" \"2020-01-22\",\n",
" datetime.datetime.now().strftime(\"%Y-%m-%d\")\n",
" )).read().decode('utf-8-sig'))\n",
" country_region_df = pd.read_csv('static_data/CountryRegion.csv')\n",
" code_to_name = {code: name for _, (name, code) in country_region_df.loc[:, ['Name', 'Code']].iterrows()}\n",
" last_df = 0\n",
" df = pd.DataFrame()\n",
" for date in sorted(data['data'].keys()):\n",
" sum_df = pd.DataFrame({name: data['data'][date][code] for code, name in code_to_name.items() if code in data['data'][date]})\n",
" sum_df = sum_df.T[['confirmed', 'deaths', 'stringency']].fillna(last_df).astype(np.float32)\n",
" tmp_df = sum_df - last_df\n",
" last_df = sum_df[['confirmed', 'deaths']]\n",
" last_df['stringency'] = 0\n",
" tmp_df = tmp_df.reset_index().rename(columns={'index': 'country_region'})\n",
" tmp_df['date'] = pd.to_datetime(date)\n",
" df = df.append(tmp_df)\n",
" return df\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## <a id=\"b2\"></a> 2. Visualize COVID-19 Data and Carbon Emission Data\n",
"\n",
"This part will guide you to create a line-column chart, where you can view the specified COVID-19 measurement (```confirmed```, ```deaths``` or ```stringency```) and carbon emissions in the specified country/region throughout time. "
]
},
{
"cell_type": "code",
2022-01-25 08:34:45 +03:00
"execution_count": 17,
2021-12-31 12:46:06 +03:00
"metadata": {},
"outputs": [],
"source": [
"def plot_covid_data_vs_carbon_emission_data(\n",
" covid_df, carbon_df, start_date=None, end_date=None,\n",
" country_region=None, sector=None, covid_measurement='confirmed',\n",
" title='Carbon Emission v.s. COVID-19 Confirmed Cases'\n",
"):\n",
" if start_date is None:\n",
" start_date = max(covid_df['date'].min(), carbon_df['date'].min())\n",
" if end_date is None:\n",
" end_date = min(covid_df['date'].max(), carbon_df['date'].max())\n",
" x = pd.to_datetime(start_date)\n",
" dates = [x]\n",
" while x <= pd.to_datetime(end_date):\n",
" x = x.replace(year=x.year+1, month=1) if x.month == 12 else x.replace(month=x.month+1)\n",
" dates.append(x)\n",
" dates = [f'{x.year}-{x.month}' for x in dates]\n",
"\n",
" plt.figure(figsize=(10, 6))\n",
" plt.title(title)\n",
" plt.xticks(rotation=60)\n",
"\n",
" if sector in set(carbon_df['sector']):\n",
" carbon_df = carbon_df[carbon_df['sector'] == sector]\n",
" else:\n",
" sector = 'All Sectors'\n",
" if 'country_region' not in carbon_df.columns:\n",
" raise ValueError('The carbon emission data need to be disaggregated by countries/regions.')\n",
" if country_region in set(carbon_df['country_region']):\n",
" carbon_df = carbon_df.loc[carbon_df['country_region'] == country_region]\n",
" else:\n",
" country_region = 'World'\n",
" carbon_df = carbon_df[['date', 'value']]\n",
" carbon_df = carbon_df.loc[(carbon_df['date'] >= f'{dates[0]}-01') & (carbon_df['date'] < f'{dates[-1]}-01')].set_index('date')\n",
" carbon_df = carbon_df.groupby(carbon_df.index.year * 12 + carbon_df.index.month).sum()\n",
" plt.bar(dates[:-1], carbon_df['value'], color='C1')\n",
" plt.ylim(0)\n",
" plt.legend([f'{country_region} {sector}\\nCarbon Emission / Mt CO2'], loc='upper left')\n",
"\n",
" plt.twinx()\n",
" if country_region in set(covid_df['country_region']):\n",
" covid_df = covid_df.loc[covid_df['country_region'] == country_region]\n",
" covid_df = covid_df[['date', covid_measurement]]\n",
" covid_df = covid_df.loc[(covid_df['date'] >= f'{dates[0]}-01') & (covid_df['date'] < f'{dates[-1]}-01')].set_index('date')\n",
" covid_df = covid_df.groupby(covid_df.index.year * 12 + covid_df.index.month)\n",
" covid_df = covid_df.mean() if covid_measurement == 'stringency' else covid_df.sum()\n",
" plt.plot(dates[:-1], covid_df[covid_measurement])\n",
" plt.ylim(0, 100 if covid_measurement == 'stringency' else None)\n",
" plt.legend([f'COVID-19\\n{covid_measurement}'], loc='upper right')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## <a id=\"b3\"></a> 3. Examples"
]
},
{
"cell_type": "code",
2022-01-25 08:34:45 +03:00
"execution_count": 18,
2021-12-31 12:46:06 +03:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2022-01-04 10:58:46 +03:00
"Download COVID-19 data\n",
"Export Data to export_data\\covid_data.csv\n"
2021-12-31 12:46:06 +03:00
]
},
{
"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>country_region</th>\n",
" <th>confirmed</th>\n",
" <th>deaths</th>\n",
" <th>stringency</th>\n",
" <th>date</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Aruba</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
2022-02-07 09:54:49 +03:00
" <td>0.000000</td>\n",
2021-12-31 12:46:06 +03:00
" <td>2020-01-22</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Afghanistan</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
2022-02-07 09:54:49 +03:00
" <td>0.000000</td>\n",
2021-12-31 12:46:06 +03:00
" <td>2020-01-22</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Angola</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
2022-02-07 09:54:49 +03:00
" <td>0.000000</td>\n",
2021-12-31 12:46:06 +03:00
" <td>2020-01-22</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Albania</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
2022-02-07 09:54:49 +03:00
" <td>0.000000</td>\n",
2021-12-31 12:46:06 +03:00
" <td>2020-01-22</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>United Arab Emirates</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
2022-02-07 09:54:49 +03:00
" <td>0.000000</td>\n",
2021-12-31 12:46:06 +03:00
" <td>2020-01-22</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>164</th>\n",
2022-02-07 09:54:49 +03:00
" <td>Vanuatu</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>19.440001</td>\n",
" <td>2022-02-03</td>\n",
2021-12-31 12:46:06 +03:00
" </tr>\n",
" <tr>\n",
" <th>165</th>\n",
2022-02-07 09:54:49 +03:00
" <td>Yemen</td>\n",
" <td>32.0</td>\n",
" <td>2.0</td>\n",
" <td>12.040000</td>\n",
" <td>2022-02-03</td>\n",
2021-12-31 12:46:06 +03:00
" </tr>\n",
" <tr>\n",
" <th>166</th>\n",
2022-02-07 09:54:49 +03:00
" <td>South Africa</td>\n",
" <td>3266.0</td>\n",
" <td>82.0</td>\n",
" <td>41.669998</td>\n",
" <td>2022-02-03</td>\n",
2021-12-31 12:46:06 +03:00
" </tr>\n",
" <tr>\n",
" <th>167</th>\n",
" <td>Zambia</td>\n",
2022-02-07 09:54:49 +03:00
" <td>388.0</td>\n",
" <td>4.0</td>\n",
" <td>37.959999</td>\n",
" <td>2022-02-03</td>\n",
2021-12-31 12:46:06 +03:00
" </tr>\n",
" <tr>\n",
" <th>168</th>\n",
" <td>Zimbabwe</td>\n",
2022-02-07 09:54:49 +03:00
" <td>158.0</td>\n",
" <td>5.0</td>\n",
" <td>40.740002</td>\n",
" <td>2022-02-03</td>\n",
2021-12-31 12:46:06 +03:00
" </tr>\n",
" </tbody>\n",
"</table>\n",
2022-02-07 09:54:49 +03:00
"<p>125060 rows × 5 columns</p>\n",
2021-12-31 12:46:06 +03:00
"</div>"
],
"text/plain": [
" country_region confirmed deaths stringency date\n",
2022-02-07 09:54:49 +03:00
"0 Aruba 0.0 0.0 0.000000 2020-01-22\n",
"1 Afghanistan 0.0 0.0 0.000000 2020-01-22\n",
"2 Angola 0.0 0.0 0.000000 2020-01-22\n",
"3 Albania 0.0 0.0 0.000000 2020-01-22\n",
"4 United Arab Emirates 0.0 0.0 0.000000 2020-01-22\n",
2021-12-31 12:46:06 +03:00
".. ... ... ... ... ...\n",
2022-02-07 09:54:49 +03:00
"164 Vanuatu 0.0 0.0 19.440001 2022-02-03\n",
"165 Yemen 32.0 2.0 12.040000 2022-02-03\n",
"166 South Africa 3266.0 82.0 41.669998 2022-02-03\n",
"167 Zambia 388.0 4.0 37.959999 2022-02-03\n",
"168 Zimbabwe 158.0 5.0 40.740002 2022-02-03\n",
2021-12-31 12:46:06 +03:00
"\n",
2022-02-07 09:54:49 +03:00
"[125060 rows x 5 columns]"
2021-12-31 12:46:06 +03:00
]
},
2022-01-25 08:34:45 +03:00
"execution_count": 18,
2021-12-31 12:46:06 +03:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(f'Download COVID-19 data')\n",
"covid_df = get_covid_data_from_oxford_covid_tracker(data_type)\n",
2022-01-04 10:58:46 +03:00
"export_data('covid_data.csv', covid_df)\n",
2021-12-31 12:46:06 +03:00
"covid_df"
]
},
{
"cell_type": "code",
2022-01-25 08:34:45 +03:00
"execution_count": 19,
2021-12-31 12:46:06 +03:00
"metadata": {},
"outputs": [
{
"data": {
2022-02-07 09:54:49 +03:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAmMAAAGUCAYAAAB5tyVBAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAACBmklEQVR4nO3dd3gbVdbA4d+R5JZenGqnV0iFNFoChN77UkIJdbOEvrRlC7Dw0XeXukAooZel9w4hCS0k4DRICOlOt9OrLel8f8zIURw7cZE0knXe59FjeTQ6c0b16N47d0RVMcYYY4wx3vB5nYAxxhhjTDqzYswYY4wxxkNWjBljjDHGeMiKMWOMMcYYD1kxZowxxhjjISvGjDHGGGM8ZMWYMRUQERWRrl7nUR0iMkJEPq3F/YeKyOxY5mRSl4j0EJECEdkgIleIyGMi8nePcunovicDXmzfmHizYswkXEWFjojcIiIvRP1/k4jMF5GNIlIoIq/uJuZZIjLZXX+ZiHwkIgfEax9iRUSeEZESN+/IZWpNYqnqi6p6eE1zUdUJqtqjpvf3kohkuq+hOSKySUQWiMjTItIxap1jRWSSe3uxiLwoIvnubTeKyPgK4ua6z09vERkpIhOjblsgIlvcYmWtiHwrIqNEpNLPVTfP1937qogcVO72JiLyrIisdC+31Ha/a+F64CtVbaiqD6rqKFW9LQZx4yJVPwOMASvGTBISkfOAc4BDVbUBMBD4YhfrXwPcD9wBtALaA/8FTqjBtr345X2PqjaIuvTzIIdU9zpwPHAW0BjoB0wBDgEQkVOBl3BeJ7lAL2AbMFFEmgIvAPuJSKdycc8ApqvqjEq2e5yqNgQ6AHcBNwBP7SbXicDZwPIKbvsPUA/oCAwGzhGR83cRa5f7XUsdgJlVWdHrFqtYfgYY4wlVtYtdEnoBFOhabtktwAvu9YeB+6sYqzGwEThtF+sMBr4D1gLL3PiZ5fIZDcwB5kctuwKYBxQB9wI+9zYf8DdgIbASeA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WsNs/hTunW4r6Lcd63uVE/Qhq5+wxOi1/5919lav0ZUMl7pQingOwVlWPjyHOpqhtU9c+q2hmnu/caEYnFgRMmjlR1PM4YvzIi0kVEPhaRKSIyQUR6ujddDDyiqmvc+66MV15WjBkvvAr8TUTyRcQnIofijDV5HcCdQuAYt6vGJyJH4YyP+qF8IFVdhzOG4xEROVFE6olIhogcJSL3uKs1xBmYu9F9k/2pinleJyJN3e6eK928wSksrhaRTiLSAOdX9qu76iZJNBEZJCJD3JalTTgDscPuVAgjRKSx23W0Hqe7qbwfcFpKrncfz4NwnqNXapjShzgF7D9xHqudtikiB4tIH3eM4HqcltKKctuBqn4OfAa8JSIDRCTgvnZGicgFbtfRtTivubNEJFtEWuN02TXC6R6LmIAzIHsM8IqqllRl50SkkYgci/P4vKCq03exbpaIZLv/Zrr5iHtbFxFpLiJ+93V/Cc7A9prs92LgW+BOdxt9gQtxBth7yu0y/xT4l/vY+dx9j3Qd/g+4wv2MaApU2upU28+Ayt4r7mv0CeA/ItLSXTcv0kIvzlQpXd3nbh1Oi/9uX68mKY0BLncL8mtxjsQFpzDvLiLfiMj3IlKlFrWasGLMeOGfOF8SE3GOELwHGKHbpw9Yj/PLdRHOF+M9wJ9UdeLOoUBV/wVcg3OE4yqcX92X4bT6gPPmOgvnCK8n2F5U7c47ONMEFOB0s0WmLHgap0ttPDAf58P78irGrMj1suM8Y0W7v8tuNcLZ1zU4LY7FON0q4AxSX+B214zC6Y7bgVuEHAcchdNC8F/gXFWdVZNk3PFhb+IMOi8bdyHOXFA3uf+2xinI1wO/4owXfN5d7zEReWwXmzgVp+B7FeeLcQbOwO/P3e2/6u731Ti
2021-12-31 12:46:06 +03:00
"text/plain": [
"<Figure size 720x432 with 2 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"plot_covid_data_vs_carbon_emission_data(\n",
" covid_df,\n",
" global_df,\n",
" start_date=None,\n",
" end_date=None,\n",
" country_region='United States',\n",
" sector=None,\n",
" covid_measurement='confirmed',\n",
" title = 'US Carbon Emission v.s. COVID-19 Confirmed Cases'\n",
")"
]
},
{
"cell_type": "code",
2022-01-25 08:34:45 +03:00
"execution_count": 20,
2021-12-31 12:46:06 +03:00
"metadata": {},
"outputs": [
{
"data": {
2022-02-07 09:54:49 +03:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAnAAAAGUCAYAAAC1Lh+qAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAABt9UlEQVR4nO3dd5hT1dbH8e9KMsPQUVHKgIJ0qdJtgIgNey8odq7X3vtrL1iuYlfsFbzXhgo2FAuKIL2ICAhKUUBQepkk+/3jnBnCMAMzmYQkM7/P8+SZzMnJyjpJTrKyz977mHMOEREREckcgVQnICIiIiKlowJOREREJMOogBMRERHJMCrgRERERDKMCjgRERGRDKMCTkRERCTDqIATKYKZOTNrmuo8SsPM+pnZZ2W4/wFmNiuROUn6M7MZZtYr1XmkmpmdbWajU52HSEmpgJMdrqjiyMxuN7PXY/6/yczmmdkaM1toZm9tJ+bpZjbeX/8PM/vYzPZP1jYkipm9bGab/LzzL1PiieWce8M5d0i8uTjnvnXOtYj3/qlkZtn+e2i2ma01s/lm9qKZNYpZ50gzG+ffvtzM3jCzBv5tN5jZN0XEre2/Pm0Kf8H7j7HezFab2T9m9r2ZXWhmxX6u+nm+7d/XFS6czKyWmb1iZkv9y+3b2e7zzOxnP4clZjbCzKr7t71sZndv77lzzrV2zn21vfUygZl9ZWbnpzoPkR1BBZykHTM7CzgT6OOcqwZ0Br7YxvpXAYOAe4E6wO7AU8AxcTx2KI6Uy+oB51y1mEv7FOSQ6d4GjgZOB2oC7YEJwEEAZnYi8Cbe+6Q20BrYCIw2s52A14F9zaxxobinAtOcc9OLedyjnHPVgT2AgcD1wAvbyXU0cAbwZxG3PQJUARoBXYE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Z4GfgaaOedq4PUrskLruK3u5X2p5Nsdr2UF/+8ehW4Ls2VhlXLOucecc53wDgs3xzs8hXPuR+fcMXiHht7H6/NU2GKgYaERlbvj9ZmLxxDgNL9YzAFGFZFvnnPuDufcXsC+eK0g/UsQeyTQNX9EaRFm4RVBJ8Uu9LftBLYcIPMK3mG+g/FaMD8swePHxuyCV8CNds797mIGp5Tk/n6Lbz/nXF3nXGu8z+hxJbhf1Dn3Bd7h8Pz9pKj39LaWb88fQK5fSOWL3UcWAPcU+jFSxTk3pASxF+D1Lds6Wed+wOvLdwDeIJXX4kt/C8vw9tnC+3hsPl8X2pZqzrl/A5hZNbxW+ReA280s3hZXkbipgJNUeAu4xcwamFnAzPoAR+GNJMyfj+kI/zBSwMwOx+vvNbZwIOfcSuBWvL4tx5pZFTPLMrPDzewBf7XqeB2V15hZS+DfJczzWjPbycwaApezeYTfEOBKM2vsf5DfC7y1rcO4O5qZdTGzbn4L1lq8vj5R86ax6GdmNZ1zeXjPS7SIEGPxWiuu85/PXniv0dA4UxqBV/TeifdcbfWYZnagmbX1D2+twmuRLSq3LTjnRuL1V3rPzDqZWch/71xoZuf6LUbX4L3nTjezHDOrCzwP1MAb7JHvW7wO+IOBoc65TSXZODOrYWZH4j0/rzvnpm1j3UpmluP/m+3nY/5tTcxsFzML+u/7AUCRU4GY2TFmdqr/HjW/H1lPvD6k4P2g2LMk+ZfQGLzW80v85/gYvEEb+Z4DLvTfd2ZmVfP34xLEfgE4x8wO8vf5XH9fzfcqXotynnOuzHO1+YeH38UrvqqY1y8ztuX7I6C5mZ3pv/+z/H0qvy/eo8B459z5eK3Lz5Q1J5HSUgEnqXAn3gi10XgjKx8A+rnNUzWswmsl+x3vy/QB4N/FfXA75/6DNzLtFrxf1guAS/Bal8D78j4db+Tcc5R8qoVheFNRTMb7kM6fHuJFvFa
2021-12-31 12:46:06 +03:00
"text/plain": [
"<Figure size 720x432 with 2 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"plot_covid_data_vs_carbon_emission_data(\n",
" covid_df,\n",
" global_df,\n",
" start_date=None,\n",
" end_date=None,\n",
" country_region='United States',\n",
" sector=None,\n",
" covid_measurement='stringency',\n",
" title = 'US Carbon Emission v.s. COVID-19 Stringency Index'\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# C. Co-Analysis of Historical Carbon Emission Data v.s. Population & GDP Data\n",
"\n",
"This section illustrates how to compare carbon intensity and per capita emissions of different countries/regions. Refer to [the EDGAR dataset](https://edgar.jrc.ec.europa.eu/dataset_ghg60) and [World Bank Open Data](https://data.worldbank.org/), carbon emissions, population and GDP data of countries/regions in the world from 1970 to 2018 are available. \n",
"\n",
"Overview:\n",
"- [Process carbon emission & social economy data](#c1)\n",
" - [Download data from EDGAR](#c11)\n",
" - [Download data from World Bank](#c12)\n",
" - [Merge datasets](#c13)\n",
"- [Visualize carbon emission & social economy data](#c2)\n",
" - [See how per capita emissions change over time in different countries/regions](#c21)\n",
" - [Observe how *carbon intensity* reduced over time](#c22)\n",
"- [Example: relationships of carbon emission and social economy in huge countries](#c3)\n",
"\n",
"*Carbon intensity* is the measure of CO2 produced per US dollar GDP. In other words, its a measure of how much CO2 we emit when we generate one dollar of domestic economy. A rapidly decreasing carbon intensity is beneficial for the environment and economy."
]
},
{
"cell_type": "code",
2022-01-25 08:34:45 +03:00
"execution_count": 21,
2021-12-31 12:46:06 +03:00
"metadata": {},
"outputs": [],
"source": [
"import zipfile\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## <a id=\"c1\"></a> 1. Process Carbon Emission & Social Economy Data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <a id=\"c11\"></a> 1.1. Download 1970-2018 yearly carbon emission data from the EDGAR dataset"
]
},
{
"cell_type": "code",
2022-01-25 08:34:45 +03:00
"execution_count": 22,
2021-12-31 12:46:06 +03:00
"metadata": {},
"outputs": [],
"source": [
"def get_historical_carbon_emission_data_from_edgar():\n",
" if not os.path.exists('download_data'):\n",
" os.mkdir('download_data')\n",
" site = 'https://cidportal.jrc.ec.europa.eu/ftp/jrc-opendata/EDGAR/datasets'\n",
" dataset = 'v60_GHG/CO2_excl_short-cycle_org_C/v60_GHG_CO2_excl_short-cycle_org_C_1970_2018.zip'\n",
" with open('download_data/historical_carbon_emission.zip', 'wb') as f:\n",
" f.write(urlopen(f'{site}/{dataset}').read())\n",
" with zipfile.ZipFile('download_data/historical_carbon_emission.zip', 'r') as zip_ref:\n",
" zip_ref.extractall('download_data/historical_carbon_emission')\n",
" hist_carbon_df = pd.read_excel(\n",
" 'download_data/historical_carbon_emission/v60_CO2_excl_short-cycle_org_C_1970_2018.xls',\n",
" sheet_name='TOTALS BY COUNTRY',\n",
" index_col=2,\n",
" header=9,\n",
" ).iloc[:, 4:]\n",
" hist_carbon_df.columns = hist_carbon_df.columns.map(lambda x: pd.to_datetime(f'{x[-4:]}-01-01'))\n",
" hist_carbon_df.index = hist_carbon_df.index.rename('country_region')\n",
" hist_carbon_df *= 1000\n",
" return hist_carbon_df\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <a id=\"c12\"></a> 1.2. Download 1960-pressent yearly population and GDP data from World Bank"
]
},
{
"cell_type": "code",
2022-01-25 08:34:45 +03:00
"execution_count": 23,
2021-12-31 12:46:06 +03:00
"metadata": {},
"outputs": [],
"source": [
"def read_worldbank_data(data_id):\n",
" tmp_df = pd.read_excel(\n",
" f'https://api.worldbank.org/v2/en/indicator/{data_id}?downloadformat=excel',\n",
" sheet_name='Data',\n",
" index_col=1,\n",
" header=3,\n",
" ).iloc[:, 3:]\n",
" tmp_df.columns = tmp_df.columns.map(lambda x: pd.to_datetime(x, format='%Y'))\n",
" tmp_df.index = tmp_df.index.rename('country_region')\n",
" return tmp_df\n",
"\n",
"def get_population_and_gdp_data_from_worldbank():\n",
" return read_worldbank_data('SP.POP.TOTL'), read_worldbank_data('NY.GDP.MKTP.CD')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <a id=\"c13\"></a> 1.3. Merge the three datasets"
]
},
{
"cell_type": "code",
2022-01-25 08:34:45 +03:00
"execution_count": 24,
2021-12-31 12:46:06 +03:00
"metadata": {},
"outputs": [],
"source": [
"def melt_table_by_years(df, value_name, country_region_codes, code_to_name, years):\n",
" return df.loc[country_region_codes, years].rename(index=code_to_name).reset_index().melt(\n",
" id_vars=['country_region'],\n",
" value_vars=years,\n",
" var_name='date',\n",
" value_name=value_name\n",
" )\n",
"\n",
"def merge_historical_data(hist_carbon_df, pop_df, gdp_df):\n",
" country_region_df = pd.read_csv('static_data/CountryRegion.csv')\n",
" code_to_name = {code: name for _, (name, code) in country_region_df.loc[:, ['Name', 'Code']].iterrows()}\n",
" country_region_codes = sorted(set(pop_df.index) & set(gdp_df.index) & set(hist_carbon_df.index) & set(code_to_name.keys()))\n",
" years = sorted(set(pop_df.columns) & set(gdp_df.columns) & set(hist_carbon_df.columns))\n",
" pop_df = melt_table_by_years(pop_df, 'population', country_region_codes, code_to_name, years)\n",
" gdp_df = melt_table_by_years(gdp_df, 'gdp', country_region_codes, code_to_name, years)\n",
" hist_carbon_df = melt_table_by_years(hist_carbon_df, 'carbon_emission', country_region_codes, code_to_name, years)\n",
" hist_carbon_df['population'] = pop_df['population']\n",
" hist_carbon_df['gdp'] = gdp_df['gdp']\n",
" return hist_carbon_df.fillna(0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## <a id=\"c2\"></a> 2. Visualize Carbon Emission & Social Economy Data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## <a id=\"c21\"></a> 2.1. Plot changes in per capita emissions\n",
"We now will walk you through how to plot a bubble chart of per capita GDP and per capita emissions of different countries/regions for a given year. "
]
},
{
"cell_type": "code",
2022-01-25 08:34:45 +03:00
"execution_count": 25,
2021-12-31 12:46:06 +03:00
"metadata": {},
"outputs": [],
"source": [
"def plot_carbon_emission_data_vs_gdp(df, year=None, countries_regions=None, title='Carbon Emission per Capita v.s. GDP per Capita'):\n",
" if year is None:\n",
" date = df['date'].max()\n",
" else:\n",
" date = min(max(pd.to_datetime(year, format='%Y'), df['date'].min()), df['date'].max())\n",
" df = df[df['date'] == date]\n",
"\n",
" if countries_regions is None or type(countries_regions) == int:\n",
" country_region_list = list(set(df['country_region']))\n",
" country_region_list.sort(key=lambda country_region: -df.loc[df['country_region'] == country_region, 'population'].to_numpy())\n",
" countries_regions = country_region_list[:10 if countries_regions is None else countries_regions]\n",
"\n",
" plt.figure(figsize=(10, 6))\n",
" plt.title(title)\n",
" max_pop = df['population'].max()\n",
" for country_region in countries_regions:\n",
" row = df.loc[df['country_region'] == country_region]\n",
" plt.scatter(\n",
" x=row['gdp'] / row['population'],\n",
" y=row['carbon_emission'] / row['population'],\n",
" s=row['population'] / max_pop * 1000,\n",
" )\n",
" for lgnd in plt.legend(countries_regions).legendHandles:\n",
" lgnd._sizes = [50]\n",
" plt.xlabel('GDP per Capita (USD)')\n",
" plt.ylabel('Carbon Emission per Capita (tCO2)')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## <a id=\"c22\"></a> 2.2. Plot changes in carbon intensity\n",
"To see changes in Carbon Intensity of different countries overtime, lets plot a line chart."
]
},
{
"cell_type": "code",
2022-01-25 08:34:45 +03:00
"execution_count": 26,
2021-12-31 12:46:06 +03:00
"metadata": {},
"outputs": [],
"source": [
"def plot_carbon_indensity_data(df, start_year=None, end_year=None, countries_regions=None, title='Carbon Indensity'):\n",
" start_date = df['date'].min() if start_year is None else pd.to_datetime(start_year, format='%Y')\n",
" end_date = df['date'].max() if end_year is None else pd.to_datetime(end_year, format='%Y')\n",
" df = df[(df['date'] >= start_date) & (df['date'] <= end_date)]\n",
"\n",
" if countries_regions is None or type(countries_regions) == int:\n",
" country_region_list = list(set(df['country_region']))\n",
" country_region_list.sort(key=lambda country_region: -df.loc[df['country_region'] == country_region, 'population'].sum())\n",
" countries_regions = country_region_list[:3 if countries_regions is None else countries_regions]\n",
" df = pd.concat([df[df['country_region'] == country_region] for country_region in countries_regions])\n",
" df['carbon_indensity'] = df['carbon_emission'] / df['gdp']\n",
" indensity_df = df.pivot(index='date', columns='country_region', values='carbon_indensity')[countries_regions]\n",
" emission_df = df.pivot(index='date', columns='country_region', values='carbon_emission')[countries_regions]\n",
"\n",
" plt.figure(figsize=(10, 8))\n",
" plt.subplot(211)\n",
" plt.title(title)\n",
" plt.plot(indensity_df)\n",
" plt.legend(countries_regions)\n",
" plt.ylabel('Carbon Emission (tCO2) per Dollar GDP')\n",
" plt.subplot(212)\n",
" plt.plot(emission_df)\n",
" plt.legend(countries_regions)\n",
" plt.ylabel('Carbon Emission (tCO2)')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## <a id=\"c3\"></a> 3. Examples"
]
},
{
"cell_type": "code",
2022-01-25 08:34:45 +03:00
"execution_count": 27,
2021-12-31 12:46:06 +03:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Download historical carbon emission data\n",
"Download population & GDP data\n",
2022-01-04 10:58:46 +03:00
"Merge data\n",
"Export Data to export_data\\historical_carbon_emission_data.csv\n"
2021-12-31 12:46:06 +03:00
]
},
{
"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>country_region</th>\n",
" <th>date</th>\n",
" <th>carbon_emission</th>\n",
" <th>population</th>\n",
" <th>gdp</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Aruba</td>\n",
" <td>1970-01-01</td>\n",
" <td>6.903589e+04</td>\n",
" <td>59070.0</td>\n",
" <td>0.000000e+00</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Afghanistan</td>\n",
" <td>1970-01-01</td>\n",
" <td>1.718179e+06</td>\n",
" <td>11173654.0</td>\n",
" <td>1.748887e+09</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Angola</td>\n",
" <td>1970-01-01</td>\n",
" <td>8.952568e+06</td>\n",
" <td>5890360.0</td>\n",
" <td>0.000000e+00</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Albania</td>\n",
" <td>1970-01-01</td>\n",
" <td>4.813550e+06</td>\n",
" <td>2135479.0</td>\n",
" <td>0.000000e+00</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>United Arab Emirates</td>\n",
" <td>1970-01-01</td>\n",
" <td>1.943758e+07</td>\n",
" <td>234512.0</td>\n",
" <td>0.000000e+00</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9109</th>\n",
" <td>Samoa</td>\n",
" <td>2018-01-01</td>\n",
" <td>3.522141e+05</td>\n",
" <td>196128.0</td>\n",
" <td>8.212869e+08</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9110</th>\n",
" <td>Yemen</td>\n",
" <td>2018-01-01</td>\n",
" <td>1.021375e+07</td>\n",
" <td>28498683.0</td>\n",
" <td>2.160614e+10</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9111</th>\n",
" <td>South Africa</td>\n",
" <td>2018-01-01</td>\n",
" <td>4.580060e+08</td>\n",
" <td>57792520.0</td>\n",
" <td>4.048421e+11</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9112</th>\n",
" <td>Zambia</td>\n",
" <td>2018-01-01</td>\n",
" <td>8.272933e+06</td>\n",
" <td>17351714.0</td>\n",
" <td>2.631159e+10</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9113</th>\n",
" <td>Zimbabwe</td>\n",
" <td>2018-01-01</td>\n",
" <td>1.286771e+07</td>\n",
" <td>14438812.0</td>\n",
" <td>1.811554e+10</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>9114 rows × 5 columns</p>\n",
"</div>"
],
"text/plain": [
" country_region date carbon_emission population \\\n",
"0 Aruba 1970-01-01 6.903589e+04 59070.0 \n",
"1 Afghanistan 1970-01-01 1.718179e+06 11173654.0 \n",
"2 Angola 1970-01-01 8.952568e+06 5890360.0 \n",
"3 Albania 1970-01-01 4.813550e+06 2135479.0 \n",
"4 United Arab Emirates 1970-01-01 1.943758e+07 234512.0 \n",
"... ... ... ... ... \n",
"9109 Samoa 2018-01-01 3.522141e+05 196128.0 \n",
"9110 Yemen 2018-01-01 1.021375e+07 28498683.0 \n",
"9111 South Africa 2018-01-01 4.580060e+08 57792520.0 \n",
"9112 Zambia 2018-01-01 8.272933e+06 17351714.0 \n",
"9113 Zimbabwe 2018-01-01 1.286771e+07 14438812.0 \n",
"\n",
" gdp \n",
"0 0.000000e+00 \n",
"1 1.748887e+09 \n",
"2 0.000000e+00 \n",
"3 0.000000e+00 \n",
"4 0.000000e+00 \n",
"... ... \n",
"9109 8.212869e+08 \n",
"9110 2.160614e+10 \n",
"9111 4.048421e+11 \n",
"9112 2.631159e+10 \n",
"9113 1.811554e+10 \n",
"\n",
"[9114 rows x 5 columns]"
]
},
2022-01-25 08:34:45 +03:00
"execution_count": 27,
2021-12-31 12:46:06 +03:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print('Download historical carbon emission data')\n",
"hist_carbon_df = get_historical_carbon_emission_data_from_edgar()\n",
"print('Download population & GDP data')\n",
"pop_df, gdp_df = get_population_and_gdp_data_from_worldbank()\n",
"print('Merge data')\n",
"hist_carbon_df = merge_historical_data(hist_carbon_df, pop_df, gdp_df)\n",
2022-01-04 10:58:46 +03:00
"export_data('historical_carbon_emission_data.csv', hist_carbon_df)\n",
2021-12-31 12:46:06 +03:00
"hist_carbon_df"
]
},
{
"cell_type": "code",
2022-01-25 08:34:45 +03:00
"execution_count": 28,
2021-12-31 12:46:06 +03:00
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAl4AAAGDCAYAAAD6aR7qAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAABnGUlEQVR4nO3dd5xU1f3/8ddn+y5VqgIqYKEvCyxgASVqRAWxoVHxZ8fYU9RvYppoYmIsiRpNUKNRE2zB3tCoYFfYpUmxIIIUpUrfvp/fH/fuOixbZsvMLsv7+XjsY2duOeczd+be+cy5555r7o6IiIiIxF5CYwcgIiIisqdQ4iUiIiISJ0q8REREROJEiZeIiIhInCjxEhEREYkTJV4iIiIicaLEqwpm5mZ2YGPHURtmNsHMXq/H+iPN7LOGjKk5MLNXzey8xo5DYqO++43Uj5nNMLOLGzuOpsTMJpvZbxs7jmjpGFk7zSbxMrOzzSzHzLaZ2TfhB2FEY8dVEzN72MwKw7jL/ubVpSx3n+Lux9Y1Fnd/19171XX9xmRmrc3sTjP7OtyGX4bPO9S3bHc/3t0fCes538zeq3/EsWNmrczsL2a2zMy2h9tkqpkNj1jGw3nbzGyDmb1pZj+qUM4MM8sPl1lvZs+Y2T7xf0Xl8cRkH6+43zTUj67wh0zZPr09LDdyP9+vvnWE9fzezD4xs2Izm1TJ/LPNbHkYw3Nm1q6asiI/F6vCz1FiQ8TZlMTyeFFDvVEdP9z9Unf/fQPV+ZqZHWtmk8ysKHy9m8zsAzM7tCHqiDxGNgYzO8/Mcs1si5mtNLNbzSwpYn47M3s2/GwvN7OzI+btY2YvmNnq8PPfvULZ7czsyfA4ud7MpphZ6/rE2ywSLzP7OXAn8EegM7Af8HfgpDqUlVTzUg3uVndv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adata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"plot_carbon_emission_data_vs_gdp(\n",
" hist_carbon_df,\n",
" year=2018,\n",
" countries_regions=10,\n",
" title = 'Carbon Emission per Capita v.s. GDP per Capita, Top 10 Populous Countries/Regions, 2018'\n",
")"
]
},
{
"cell_type": "code",
2022-01-25 08:34:45 +03:00
"execution_count": 29,
2021-12-31 12:46:06 +03:00
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAnYAAAHiCAYAAABlS4ExAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAACdvElEQVR4nOzdd3xUZdbA8d9JhyS00DtSpNdQVFSsYEfFir2ta9d1X3V1Fd1113Xdte+6trVX7HUVGygghCJVpUPokJDeZnLeP54bGELKEGYyKef7YT4zc8tzz+Qm4eSpoqoYY4wxxpj6LyrSARhjjDHGmNCwxM4YY4wxpoGwxM4YY4wxpoGwxM4YY4wxpoGwxM4YY4wxpoGwxM4YY4wxpoGwxM7UaSKiItIr0nFURUTGiUh6mK/xBxF5NpzXqG218XULBxF5SkT+eADnN7h7WRuq+3450PtiTENhiZ0JKRE5X0TSRCRXRDaLyGciMjbScVVHRF4QkT9HOo7KqOpfVPUKABHp7iW8MTUtT0R6iMh3IpIjIutE5KIgzokTkSkiskJE8kRkrYg8LyLdaxpHbfFiLfC+L8seT9SkLFW9WlX/VNNYAu9lOInItyJyRblteyVHInKaiCwUkWwR2SEiX4tIj3DHVhkRGSUin4rILhHJEJE5InJpMOce6H3ZjxgHisj/vK/XPhPBikg/7+uYJSIrReT0gH2Ty30P5ns/yyO8/SIifxORnd7jbyIiVcTykPfzmCMiP5f/ORaRoSIyz7vOPBEZGrDvKBH5xotzbQVlDxWRGd7+dEua6w9L7EzIiMgtwCPAX4B2QFfgX8BpNSirx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
"<Figure size 720x576 with 2 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"plot_carbon_indensity_data(\n",
" hist_carbon_df,\n",
" start_year=None,\n",
" end_year=None,\n",
" countries_regions=['United States', 'China'],\n",
" title='Carbon Indensity & Carbon Emission, US v.s. China, 1970-2018'\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}