{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2f9b5f12",
   "metadata": {},
   "source": [
    "# Aggregation\n",
    "\n",
    "This notebook covers both classic workbook-based aggregation and workbook-free regional aggregation through `region_aggregation`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f661e804",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-04-26T22:40:43.709274Z",
     "iopub.status.busy": "2026-04-26T22:40:43.708895Z",
     "iopub.status.idle": "2026-04-26T22:40:45.927168Z",
     "shell.execute_reply": "2026-04-26T22:40:45.926787Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO Parser: excel reading IOT flows from /Users/lorenzorinaldi/Documents/GitHub/MARIO/mario/test/tables/test_IOT_standard.xlsx.\n",
      "INFO Parser: state payload ready with 6 canonical blocks.\n",
      "INFO Parser: excel state ready for IOT.\n",
      "INFO Metadata: initialized.\n"
     ]
    }
   ],
   "source": [
    "import mario\n",
    "db = mario.load_test(\"IOT\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "efc9d17a",
   "metadata": {},
   "source": [
    "## Generate an empty Excel template\n",
    "\n",
    "Open the generated workbook if you want to see the format MARIO expects."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5bc80f6a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-04-26T22:40:46.038386Z",
     "iopub.status.busy": "2026-04-26T22:40:46.038254Z",
     "iopub.status.idle": "2026-04-26T22:40:46.047907Z",
     "shell.execute_reply": "2026-04-26T22:40:46.047634Z"
    }
   },
   "outputs": [],
   "source": [
    "db.get_aggregation_excel(\n",
    "    path=\"/path/to/empty_aggregation_template.xlsx\",\n",
    "    overwrite=True,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cedd2231",
   "metadata": {},
   "source": [
    "## Apply a filled Excel workbook\n",
    "\n",
    "![The add-sectors cluster sheets](../../_static/images/aggregation_template.png)\n",
    "\n",
    "The example workbook groups `Factors of production` as \"Value added\", and `Regions` into just one \"World\" region. The other levels are left blank, since they don't need to be filled if the user's intention is to leave them untouched."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "739925e7",
   "metadata": {},
   "source": [
    "### Download the packaged example workbooks\n",
    "\n",
    "The exact workbooks used in this example are available here:\n",
    "\n",
    "- [Empty aggregation template](../../_static/data/supporting_files/aggregation_iot_template.xlsx)\n",
    "- [Filled aggregation workbook](../../_static/data/supporting_files/aggregation_iot_filled.xlsx)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "5061e83a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-04-26T22:40:46.049647Z",
     "iopub.status.busy": "2026-04-26T22:40:46.049534Z",
     "iopub.status.idle": "2026-04-26T22:40:46.299090Z",
     "shell.execute_reply": "2026-04-26T22:40:46.298815Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING nan values for the aggregation of Satellite account for following items ignored\n",
      "['Employment', 'CO2']\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING nan values for the aggregation of Consumption category for following items ignored\n",
      "['Final demand']\n",
      "WARNING nan values for the aggregation of Sector for following items ignored\n",
      "['Agriculture', 'Services', 'Industry']\n",
      "INFO Resolver: resolving X for baseline.\n",
      "INFO Resolver: trying X via formula build_iot_X_from_Z_Y.\n",
      "INFO Resolver: resolved X via formula build_iot_X_from_Z_Y.\n",
      "INFO Aggregation: scenario: `baseline` aggregated.\n",
      "INFO Resolver: resolving z for baseline.\n",
      "INFO Resolver: trying z via formula build_iot_z_from_Z_X.\n",
      "INFO Resolver: resolved z via formula build_iot_z_from_Z_X.\n",
      "INFO Resolver: resolving v for baseline.\n",
      "INFO Resolver: trying v via formula build_iot_v_from_V_X.\n",
      "INFO Resolver: resolved v via formula build_iot_v_from_V_X.\n",
      "INFO Resolver: resolving e for baseline.\n",
      "INFO Resolver: trying e via formula build_iot_e_from_E_X.\n",
      "INFO Resolver: resolved e via formula build_iot_e_from_E_X.\n"
     ]
    }
   ],
   "source": [
    "aggregated_db = db.aggregate(\n",
    "    \"/path/to/aggregation_iot_filled.xlsx\",\n",
    "    inplace=False,\n",
    "    ignore_nan=True, # if you didn't aggregate all labels in all sets, this will ignore empty cells.\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e9ccfefa",
   "metadata": {},
   "source": [
    "## Inspect the aggregated table"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "336869e9",
   "metadata": {},
   "source": [
    "Compare aggregated and non-aggregated databases "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "2edb2e15",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "name = IOT test (standard)\n",
       "table = IOT\n",
       "scenarios = ['baseline']\n",
       "Factor of production = 1\n",
       "Satellite account = 2\n",
       "Consumption category = 1\n",
       "Region = 1\n",
       "Sector = 3"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "aggregated_db"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "03e93a27",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "name = IOT test (standard)\n",
       "table = IOT\n",
       "scenarios = ['baseline']\n",
       "Factor of production = 3\n",
       "Satellite account = 2\n",
       "Consumption category = 1\n",
       "Region = 2\n",
       "Sector = 3"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6071f26",
   "metadata": {},
   "source": [
    "Let's also obtain the Z matrix for the aggregated database and compare it with the one of the non-aggregated database"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "39ebd52f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-04-26T22:40:46.300844Z",
     "iopub.status.busy": "2026-04-26T22:40:46.300726Z",
     "iopub.status.idle": "2026-04-26T22:40:46.329308Z",
     "shell.execute_reply": "2026-04-26T22:40:46.329051Z"
    }
   },
   "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 tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Region</th>\n",
       "      <th colspan=\"3\" halign=\"left\">World</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Level</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Sector</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Item</th>\n",
       "      <th>Agriculture</th>\n",
       "      <th>Industry</th>\n",
       "      <th>Services</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Region</th>\n",
       "      <th>Level</th>\n",
       "      <th>Item</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">World</th>\n",
       "      <th rowspan=\"3\" valign=\"top\">Sector</th>\n",
       "      <th>Agriculture</th>\n",
       "      <td>9.366075e+05</td>\n",
       "      <td>5.441638e+06</td>\n",
       "      <td>1.094425e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Industry</th>\n",
       "      <td>1.140438e+06</td>\n",
       "      <td>2.344857e+07</td>\n",
       "      <td>1.090801e+07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Services</th>\n",
       "      <td>1.913158e+06</td>\n",
       "      <td>1.009323e+07</td>\n",
       "      <td>3.141690e+07</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Region                            World                            \n",
       "Level                            Sector                            \n",
       "Item                        Agriculture      Industry      Services\n",
       "Region Level  Item                                                 \n",
       "World  Sector Agriculture  9.366075e+05  5.441638e+06  1.094425e+06\n",
       "              Industry     1.140438e+06  2.344857e+07  1.090801e+07\n",
       "              Services     1.913158e+06  1.009323e+07  3.141690e+07"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "aggregated_db.Z"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "cc25df1b",
   "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 tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Region</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Reg1</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Reg2</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Level</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Sector</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Sector</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Item</th>\n",
       "      <th>Agriculture</th>\n",
       "      <th>Industry</th>\n",
       "      <th>Services</th>\n",
       "      <th>Agriculture</th>\n",
       "      <th>Industry</th>\n",
       "      <th>Services</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Region</th>\n",
       "      <th>Level</th>\n",
       "      <th>Item</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">Reg1</th>\n",
       "      <th rowspan=\"3\" valign=\"top\">Sector</th>\n",
       "      <th>Agriculture</th>\n",
       "      <td>9.308651e+05</td>\n",
       "      <td>5.357799e+06</td>\n",
       "      <td>1.075278e+06</td>\n",
       "      <td>2363.029548</td>\n",
       "      <td>50607.806861</td>\n",
       "      <td>10341.916110</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Industry</th>\n",
       "      <td>1.122397e+06</td>\n",
       "      <td>2.289284e+07</td>\n",
       "      <td>1.065096e+07</td>\n",
       "      <td>1413.697219</td>\n",
       "      <td>134910.657543</td>\n",
       "      <td>30367.209709</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Services</th>\n",
       "      <td>1.893586e+06</td>\n",
       "      <td>9.737509e+06</td>\n",
       "      <td>3.059785e+07</td>\n",
       "      <td>1301.725200</td>\n",
       "      <td>27813.857216</td>\n",
       "      <td>58525.384254</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">Reg2</th>\n",
       "      <th rowspan=\"3\" valign=\"top\">Sector</th>\n",
       "      <th>Agriculture</th>\n",
       "      <td>4.168587e+02</td>\n",
       "      <td>2.596041e+03</td>\n",
       "      <td>8.430071e+02</td>\n",
       "      <td>2962.557738</td>\n",
       "      <td>30635.045696</td>\n",
       "      <td>7961.643900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Industry</th>\n",
       "      <td>6.890987e+03</td>\n",
       "      <td>1.335193e+05</td>\n",
       "      <td>6.555829e+04</td>\n",
       "      <td>9736.931972</td>\n",
       "      <td>287300.673355</td>\n",
       "      <td>161126.940614</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Services</th>\n",
       "      <td>1.590090e+03</td>\n",
       "      <td>1.555045e+04</td>\n",
       "      <td>3.843707e+04</td>\n",
       "      <td>16680.335207</td>\n",
       "      <td>312355.775242</td>\n",
       "      <td>722089.042972</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Region                             Reg1                              \\\n",
       "Level                            Sector                               \n",
       "Item                        Agriculture      Industry      Services   \n",
       "Region Level  Item                                                    \n",
       "Reg1   Sector Agriculture  9.308651e+05  5.357799e+06  1.075278e+06   \n",
       "              Industry     1.122397e+06  2.289284e+07  1.065096e+07   \n",
       "              Services     1.893586e+06  9.737509e+06  3.059785e+07   \n",
       "Reg2   Sector Agriculture  4.168587e+02  2.596041e+03  8.430071e+02   \n",
       "              Industry     6.890987e+03  1.335193e+05  6.555829e+04   \n",
       "              Services     1.590090e+03  1.555045e+04  3.843707e+04   \n",
       "\n",
       "Region                             Reg2                                \n",
       "Level                            Sector                                \n",
       "Item                        Agriculture       Industry       Services  \n",
       "Region Level  Item                                                     \n",
       "Reg1   Sector Agriculture   2363.029548   50607.806861   10341.916110  \n",
       "              Industry      1413.697219  134910.657543   30367.209709  \n",
       "              Services      1301.725200   27813.857216   58525.384254  \n",
       "Reg2   Sector Agriculture   2962.557738   30635.045696    7961.643900  \n",
       "              Industry      9736.931972  287300.673355  161126.940614  \n",
       "              Services     16680.335207  312355.775242  722089.042972  "
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db.Z"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e4fe4aae",
   "metadata": {},
   "source": [
    "## Workbook-free regional aggregation for known MRIOs\n",
    "\n",
    "The same aggregation method can bypass the use of an Excel template if the user wants just to aggregate the `Region` level.\n",
    "This is possible when using \"MARIO-known\" MRIO tables (e.g. EXIOBASE, EORA, OECD-ICIO...), thanks to an internal mapping process MARIO performs with built-in coverage rules, without asking you to fill the `Region` sheet manually.\n",
    "\n",
    "The mapping is done in two steps. First, MARIO uses its packaged [Country_coverage workbook](../../_static/data/supporting_files/Country_coverage.xlsx) to reconcile source-specific region codes with ISO3 labels. Then, for labels that still need to be resolved, it falls back to [`country_converter`](https://github.com/IndEcol/country_converter), which provides the country metadata used to assign preset groups such as continents, UN regions, `EU`, `OECD`, `G7`, and `G20`.\n",
    "\n",
    "For `region_aggregation`, the accepted preset strings are `\"continent\"`, `\"UNregion\"`, `\"EU\"`, `\"OECD\"`, `\"G7\"`, and `\"G20\"`. You can also pass an explicit mapping as a `dict`, `pandas.Series`, or `pandas.DataFrame` if you want full control over the target Region labels.\n",
    "\n",
    "Below, a guided example using EXIOBASE."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "72861449",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['AT',\n",
       " 'BE',\n",
       " 'BG',\n",
       " 'CY',\n",
       " 'CZ',\n",
       " 'DE',\n",
       " 'DK',\n",
       " 'EE',\n",
       " 'ES',\n",
       " 'FI',\n",
       " 'FR',\n",
       " 'GR',\n",
       " 'HR',\n",
       " 'HU',\n",
       " 'IE',\n",
       " 'IT',\n",
       " 'LT',\n",
       " 'LU',\n",
       " 'LV',\n",
       " 'MT',\n",
       " 'NL',\n",
       " 'PL',\n",
       " 'PT',\n",
       " 'RO',\n",
       " 'SE',\n",
       " 'SI',\n",
       " 'SK',\n",
       " 'GB',\n",
       " 'US',\n",
       " 'JP',\n",
       " 'CN',\n",
       " 'CA',\n",
       " 'KR',\n",
       " 'BR',\n",
       " 'IN',\n",
       " 'MX',\n",
       " 'RU',\n",
       " 'AU',\n",
       " 'CH',\n",
       " 'TR',\n",
       " 'TW',\n",
       " 'NO',\n",
       " 'ID',\n",
       " 'ZA',\n",
       " 'WA',\n",
       " 'WL',\n",
       " 'WE',\n",
       " 'WF',\n",
       " 'WM']"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import mario\n",
    "\n",
    "mario.set_log_verbosity(\"critical\")\n",
    "\n",
    "exio_db = mario.parse_exiobase(\n",
    "    path=\"/path/to/IOT_2024_ixi.zip\",  # or the extracted dataset directory\n",
    "    table=\"IOT\",\n",
    "    unit=\"Monetary\",\n",
    ")\n",
    "\n",
    "exio_db.regions  # print regions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "123cf75e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['America', 'Asia', 'Oceania', 'Africa', 'Europe']"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "exio_by_continent = exio_db.aggregate(\n",
    "    io=None,\n",
    "    levels=\"Region\",\n",
    "    region_aggregation=\"continent\",\n",
    "    inplace=False,\n",
    ")\n",
    "\n",
    "exio_by_continent.regions"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "735f3c87",
   "metadata": {},
   "source": [
    "Another example below, using OECD-ICIO tables, aggregating `\"G20\"` countries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "f39f4bca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "name = OECD ICIO 2022 regular\n",
       "table = IOT\n",
       "scenarios = ['baseline']\n",
       "Factor of production = 2\n",
       "Satellite account = 1\n",
       "Consumption category = 6\n",
       "Region = 81\n",
       "Sector = 50"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "oecd_db = mario.parse_oecd(\n",
    "    path=\"/path/to/OECD_ICIO/folder_or_csv\",\n",
    ")\n",
    "\n",
    "oecd_db"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "a595ff91",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['AGO',\n",
       " 'ARE',\n",
       " 'ARG',\n",
       " 'AUS',\n",
       " 'AUT',\n",
       " 'BEL',\n",
       " 'BGD',\n",
       " 'BGR',\n",
       " 'BLR',\n",
       " 'BRA',\n",
       " 'BRN',\n",
       " 'CAN',\n",
       " 'CHE',\n",
       " 'CHL',\n",
       " 'CHN',\n",
       " 'CIV',\n",
       " 'CMR',\n",
       " 'COD',\n",
       " 'COL',\n",
       " 'CRI',\n",
       " 'CYP',\n",
       " 'CZE',\n",
       " 'DEU',\n",
       " 'DNK',\n",
       " 'EGY',\n",
       " 'ESP',\n",
       " 'EST',\n",
       " 'FIN',\n",
       " 'FRA',\n",
       " 'GBR',\n",
       " 'GRC',\n",
       " 'HKG',\n",
       " 'HRV',\n",
       " 'HUN',\n",
       " 'IDN',\n",
       " 'IND',\n",
       " 'IRL',\n",
       " 'ISL',\n",
       " 'ISR',\n",
       " 'ITA',\n",
       " 'JOR',\n",
       " 'JPN',\n",
       " 'KAZ',\n",
       " 'KHM',\n",
       " 'KOR',\n",
       " 'LAO',\n",
       " 'LTU',\n",
       " 'LUX',\n",
       " 'LVA',\n",
       " 'MAR',\n",
       " 'MEX',\n",
       " 'MLT',\n",
       " 'MMR',\n",
       " 'MYS',\n",
       " 'NGA',\n",
       " 'NLD',\n",
       " 'NOR',\n",
       " 'NZL',\n",
       " 'PAK',\n",
       " 'PER',\n",
       " 'PHL',\n",
       " 'POL',\n",
       " 'PRT',\n",
       " 'ROU',\n",
       " 'RUS',\n",
       " 'SAU',\n",
       " 'SEN',\n",
       " 'SGP',\n",
       " 'STP',\n",
       " 'SVK',\n",
       " 'SVN',\n",
       " 'SWE',\n",
       " 'THA',\n",
       " 'TUN',\n",
       " 'TUR',\n",
       " 'TWN',\n",
       " 'UKR',\n",
       " 'USA',\n",
       " 'VNM',\n",
       " 'ZAF',\n",
       " 'ROW']"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "oecd_db.regions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "8fa211af",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "name = OECD ICIO 2022 regular\n",
       "table = IOT\n",
       "scenarios = ['baseline']\n",
       "Factor of production = 2\n",
       "Satellite account = 1\n",
       "Consumption category = 6\n",
       "Region = 39\n",
       "Sector = 50"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "oecd_by_g20 = oecd_db.aggregate(\n",
    "    io = None,\n",
    "    levels=\"Region\",\n",
    "    region_aggregation=\"G20\",\n",
    "    inplace=False,\n",
    ")\n",
    "\n",
    "oecd_by_g20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "704a4680",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['G20',\n",
       " 'SGP',\n",
       " 'ARE',\n",
       " 'THA',\n",
       " 'COL',\n",
       " 'NZL',\n",
       " 'COD',\n",
       " 'PER',\n",
       " 'KAZ',\n",
       " 'CHL',\n",
       " 'STP',\n",
       " 'NGA',\n",
       " 'BGD',\n",
       " 'TWN',\n",
       " 'ROW',\n",
       " 'ISR',\n",
       " 'UKR',\n",
       " 'EGY',\n",
       " 'CIV',\n",
       " 'HKG',\n",
       " 'ISL',\n",
       " 'KHM',\n",
       " 'BRN',\n",
       " 'CHE',\n",
       " 'CRI',\n",
       " 'JOR',\n",
       " 'LAO',\n",
       " 'NOR',\n",
       " 'VNM',\n",
       " 'MMR',\n",
       " 'PAK',\n",
       " 'MAR',\n",
       " 'SEN',\n",
       " 'TUN',\n",
       " 'BLR',\n",
       " 'AGO',\n",
       " 'CMR',\n",
       " 'PHL',\n",
       " 'MYS']"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "oecd_by_g20.regions"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "mario",
   "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.13.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
