{ "cells": [ { "cell_type": "markdown", "id": "6d3cfbc9-bc67-432f-badc-1daec4cb4779", "metadata": {}, "source": [ "# Extract data from Pymrio" ] }, { "cell_type": "markdown", "id": "bcb8ce71-6b45-4ccc-81ad-866bc0d6c6f8", "metadata": {}, "source": [ "This notebook shows how to extract specific data from the pymrio object for further processing in Python. For exporting/saving the data to another file format see [the notebook on saving/loading/exporting data.](./load_save_export.ipynb)" ] }, { "cell_type": "code", "execution_count": 39, "id": "4f298de3-3641-44bb-88bb-c428d9cf5693", "metadata": { "tags": [] }, "outputs": [], "source": [ "import pymrio" ] }, { "cell_type": "code", "execution_count": 40, "id": "a38719d5-011c-4dff-969e-f915a205fd44", "metadata": { "tags": [] }, "outputs": [], "source": [ "mrio = pymrio.load_test().calc_all()\n", "\n", "### Basic pandas indexing of pymrio tables" ] }, { "cell_type": "markdown", "id": "f50f0173", "metadata": {}, "source": [ "Since pymrio is built on top of pandas, we can use the pandas functions to extract data from the pymrio object. For example, to access the part of the A matrix from the region 2 we can use:" ] }, { "cell_type": "code", "execution_count": 41, "id": "f6aadba4-ef17-40d2-aaec-845e274026b7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sectorfoodminingmanufactoringelectricityconstructiontradetransportother
sector
food0.0004860.0006380.0001940.0000050.0000190.0000920.0000270.000026
mining0.0000060.0509040.0000470.0002180.0002030.0000110.0000100.000013
manufactoring0.0004880.0698620.0015290.0001960.0059150.0011910.0022940.000844
electricity0.0000890.0504270.0001370.0006040.0001460.0001770.0002800.000248
construction0.0000250.0073750.0000320.0001090.0046150.0000880.0005150.000422
trade0.0002510.0287700.0005310.0000950.0016400.0007720.0013720.000487
transport0.0000710.0318390.0002120.0000690.0007140.0005790.0047470.000494
other0.0001710.0649350.0005950.0002910.0028440.0018970.0038000.003936
\n", "
" ], "text/plain": [ "sector food mining manufactoring electricity construction \\\n", "sector \n", "food 0.000486 0.000638 0.000194 0.000005 0.000019 \n", "mining 0.000006 0.050904 0.000047 0.000218 0.000203 \n", "manufactoring 0.000488 0.069862 0.001529 0.000196 0.005915 \n", "electricity 0.000089 0.050427 0.000137 0.000604 0.000146 \n", "construction 0.000025 0.007375 0.000032 0.000109 0.004615 \n", "trade 0.000251 0.028770 0.000531 0.000095 0.001640 \n", "transport 0.000071 0.031839 0.000212 0.000069 0.000714 \n", "other 0.000171 0.064935 0.000595 0.000291 0.002844 \n", "\n", "sector trade transport other \n", "sector \n", "food 0.000092 0.000027 0.000026 \n", "mining 0.000011 0.000010 0.000013 \n", "manufactoring 0.001191 0.002294 0.000844 \n", "electricity 0.000177 0.000280 0.000248 \n", "construction 0.000088 0.000515 0.000422 \n", "trade 0.000772 0.001372 0.000487 \n", "transport 0.000579 0.004747 0.000494 \n", "other 0.001897 0.003800 0.003936 " ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A_reg2 = mrio.A.loc[\"reg2\", \"reg2\"]\n", "A_reg2" ] }, { "cell_type": "markdown", "id": "1a9e6bcd", "metadata": { "lines_to_next_cell": 2 }, "source": [ "Most tables are indexed via a multiindex, in case of the A matrix the index is a tuple of the region and the sector.\n", "To access all technical coefficients (column) data for mining from all regions we can use:" ] }, { "cell_type": "code", "execution_count": 42, "id": "bdc4c511", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
regionreg1reg2reg3reg4reg5reg6
sectorminingminingminingminingminingmining
regionsector
reg1food0.0011790.0000103.652734e-091.626677e-063.767567e-071.481621e-05
mining0.0480220.0002687.486558e-076.899387e-053.628651e-058.801658e-05
manufactoring0.1243660.0174172.799765e-059.161688e-034.792741e-032.087445e-03
electricity0.0379910.0010992.170169e-071.150382e-081.444660e-058.892062e-06
construction0.0173240.0000228.884210e-081.331990e-071.129186e-057.641830e-07
trade0.0354290.0018366.170323e-073.835340e-041.071471e-035.575273e-04
transport0.0603240.0015444.060594e-065.820972e-041.278089e-036.555200e-03
other0.0920590.0050241.788858e-053.664017e-047.664473e-043.287131e-04
reg2food0.0000840.0006383.772203e-092.165695e-079.237430e-083.105702e-05
mining0.0005230.0509045.883755e-065.472492e-052.212937e-053.108304e-05
manufactoring0.0145630.0698621.223889e-054.170428e-032.164157e-031.355740e-03
electricity0.0004300.0504271.143580e-082.061508e-083.266417e-063.535380e-07
construction0.0000500.0073751.666888e-071.910407e-071.702833e-051.421644e-06
trade0.0007690.0287701.092766e-061.792846e-041.000066e-033.103879e-04
transport0.0030950.0318391.993361e-062.990600e-051.294761e-031.368746e-03
other0.0015840.0649352.418065e-052.702272e-044.571674e-043.392091e-04
reg3food0.0000370.0000051.309056e-056.378397e-064.673894e-073.461944e-06
mining0.0003960.0001514.337922e-043.495268e-041.564949e-044.882839e-05
manufactoring0.0077820.0038426.572771e-045.043959e-033.621657e-031.629835e-03
electricity0.0000450.0001072.495370e-041.401577e-045.464145e-071.165971e-05
construction0.0000060.0000032.786913e-042.624508e-076.239448e-061.237150e-08
trade0.0001500.0000602.485529e-044.141811e-051.517855e-032.062017e-03
transport0.0017680.0006182.091958e-044.965269e-043.777028e-042.449723e-03
other0.0022690.0075831.861799e-032.275626e-032.430304e-031.329935e-03
reg4food0.0001710.0005786.340547e-101.413307e-031.378513e-072.636256e-05
mining0.0003110.0024672.719214e-056.138652e-021.552800e-042.699482e-04
manufactoring0.0052070.0035794.405268e-051.493263e-012.139451e-032.578051e-03
electricity0.0000810.0000131.295279e-064.967797e-021.543713e-081.669269e-05
construction0.0000210.0000086.055807e-072.337226e-037.889622e-062.033545e-06
trade0.0000050.0000161.595023e-072.826377e-029.701854e-057.025049e-04
transport0.0003560.0000489.595997e-071.586496e-023.012550e-041.991747e-03
other0.0008730.0009586.314447e-064.648443e-021.117863e-046.261181e-04
reg5food0.0000620.0000281.108149e-071.524618e-051.996382e-042.327498e-05
mining0.0031430.0017842.499451e-061.013051e-035.149154e-029.996927e-04
manufactoring0.0046800.0023651.612636e-055.466745e-048.061924e-021.405121e-03
electricity0.0009560.0000185.676661e-082.164907e-062.545719e-021.096431e-05
construction0.0000710.0000345.621276e-073.027990e-081.765549e-023.883278e-06
trade0.0001490.0006391.895713e-065.016841e-054.981458e-021.014394e-03
transport0.0011540.0008202.183364e-062.255475e-053.801613e-022.591267e-03
other0.0003470.0005124.026853e-069.871058e-056.984674e-025.676656e-04
reg6food0.0001430.0000931.099390e-076.355405e-051.119791e-066.959613e-04
mining0.0074560.0055673.220657e-052.052196e-039.791972e-041.512044e-01
manufactoring0.0058410.0057842.046172e-052.154067e-036.448726e-035.043697e-02
electricity0.0001970.0001681.470729e-071.102498e-053.010193e-056.787911e-03
construction0.0000380.0000235.264129e-075.950288e-073.829687e-052.041605e-03
trade0.0002090.0001846.734943e-071.334155e-041.324385e-044.701872e-03
transport0.0026360.0023084.652734e-064.705530e-052.978642e-044.032717e-02
other0.0005050.0008043.262233e-062.790342e-042.084267e-042.052462e-02
\n", "
" ], "text/plain": [ "region reg1 reg2 reg3 reg4 \\\n", "sector mining mining mining mining \n", "region sector \n", "reg1 food 0.001179 0.000010 3.652734e-09 1.626677e-06 \n", " mining 0.048022 0.000268 7.486558e-07 6.899387e-05 \n", " manufactoring 0.124366 0.017417 2.799765e-05 9.161688e-03 \n", " electricity 0.037991 0.001099 2.170169e-07 1.150382e-08 \n", " construction 0.017324 0.000022 8.884210e-08 1.331990e-07 \n", " trade 0.035429 0.001836 6.170323e-07 3.835340e-04 \n", " transport 0.060324 0.001544 4.060594e-06 5.820972e-04 \n", " other 0.092059 0.005024 1.788858e-05 3.664017e-04 \n", "reg2 food 0.000084 0.000638 3.772203e-09 2.165695e-07 \n", " mining 0.000523 0.050904 5.883755e-06 5.472492e-05 \n", " manufactoring 0.014563 0.069862 1.223889e-05 4.170428e-03 \n", " electricity 0.000430 0.050427 1.143580e-08 2.061508e-08 \n", " construction 0.000050 0.007375 1.666888e-07 1.910407e-07 \n", " trade 0.000769 0.028770 1.092766e-06 1.792846e-04 \n", " transport 0.003095 0.031839 1.993361e-06 2.990600e-05 \n", " other 0.001584 0.064935 2.418065e-05 2.702272e-04 \n", "reg3 food 0.000037 0.000005 1.309056e-05 6.378397e-06 \n", " mining 0.000396 0.000151 4.337922e-04 3.495268e-04 \n", " manufactoring 0.007782 0.003842 6.572771e-04 5.043959e-03 \n", " electricity 0.000045 0.000107 2.495370e-04 1.401577e-04 \n", " construction 0.000006 0.000003 2.786913e-04 2.624508e-07 \n", " trade 0.000150 0.000060 2.485529e-04 4.141811e-05 \n", " transport 0.001768 0.000618 2.091958e-04 4.965269e-04 \n", " other 0.002269 0.007583 1.861799e-03 2.275626e-03 \n", "reg4 food 0.000171 0.000578 6.340547e-10 1.413307e-03 \n", " mining 0.000311 0.002467 2.719214e-05 6.138652e-02 \n", " manufactoring 0.005207 0.003579 4.405268e-05 1.493263e-01 \n", " electricity 0.000081 0.000013 1.295279e-06 4.967797e-02 \n", " construction 0.000021 0.000008 6.055807e-07 2.337226e-03 \n", " trade 0.000005 0.000016 1.595023e-07 2.826377e-02 \n", " transport 0.000356 0.000048 9.595997e-07 1.586496e-02 \n", " other 0.000873 0.000958 6.314447e-06 4.648443e-02 \n", "reg5 food 0.000062 0.000028 1.108149e-07 1.524618e-05 \n", " mining 0.003143 0.001784 2.499451e-06 1.013051e-03 \n", " manufactoring 0.004680 0.002365 1.612636e-05 5.466745e-04 \n", " electricity 0.000956 0.000018 5.676661e-08 2.164907e-06 \n", " construction 0.000071 0.000034 5.621276e-07 3.027990e-08 \n", " trade 0.000149 0.000639 1.895713e-06 5.016841e-05 \n", " transport 0.001154 0.000820 2.183364e-06 2.255475e-05 \n", " other 0.000347 0.000512 4.026853e-06 9.871058e-05 \n", "reg6 food 0.000143 0.000093 1.099390e-07 6.355405e-05 \n", " mining 0.007456 0.005567 3.220657e-05 2.052196e-03 \n", " manufactoring 0.005841 0.005784 2.046172e-05 2.154067e-03 \n", " electricity 0.000197 0.000168 1.470729e-07 1.102498e-05 \n", " construction 0.000038 0.000023 5.264129e-07 5.950288e-07 \n", " trade 0.000209 0.000184 6.734943e-07 1.334155e-04 \n", " transport 0.002636 0.002308 4.652734e-06 4.705530e-05 \n", " other 0.000505 0.000804 3.262233e-06 2.790342e-04 \n", "\n", "region reg5 reg6 \n", "sector mining mining \n", "region sector \n", "reg1 food 3.767567e-07 1.481621e-05 \n", " mining 3.628651e-05 8.801658e-05 \n", " manufactoring 4.792741e-03 2.087445e-03 \n", " electricity 1.444660e-05 8.892062e-06 \n", " construction 1.129186e-05 7.641830e-07 \n", " trade 1.071471e-03 5.575273e-04 \n", " transport 1.278089e-03 6.555200e-03 \n", " other 7.664473e-04 3.287131e-04 \n", "reg2 food 9.237430e-08 3.105702e-05 \n", " mining 2.212937e-05 3.108304e-05 \n", " manufactoring 2.164157e-03 1.355740e-03 \n", " electricity 3.266417e-06 3.535380e-07 \n", " construction 1.702833e-05 1.421644e-06 \n", " trade 1.000066e-03 3.103879e-04 \n", " transport 1.294761e-03 1.368746e-03 \n", " other 4.571674e-04 3.392091e-04 \n", "reg3 food 4.673894e-07 3.461944e-06 \n", " mining 1.564949e-04 4.882839e-05 \n", " manufactoring 3.621657e-03 1.629835e-03 \n", " electricity 5.464145e-07 1.165971e-05 \n", " construction 6.239448e-06 1.237150e-08 \n", " trade 1.517855e-03 2.062017e-03 \n", " transport 3.777028e-04 2.449723e-03 \n", " other 2.430304e-03 1.329935e-03 \n", "reg4 food 1.378513e-07 2.636256e-05 \n", " mining 1.552800e-04 2.699482e-04 \n", " manufactoring 2.139451e-03 2.578051e-03 \n", " electricity 1.543713e-08 1.669269e-05 \n", " construction 7.889622e-06 2.033545e-06 \n", " trade 9.701854e-05 7.025049e-04 \n", " transport 3.012550e-04 1.991747e-03 \n", " other 1.117863e-04 6.261181e-04 \n", "reg5 food 1.996382e-04 2.327498e-05 \n", " mining 5.149154e-02 9.996927e-04 \n", " manufactoring 8.061924e-02 1.405121e-03 \n", " electricity 2.545719e-02 1.096431e-05 \n", " construction 1.765549e-02 3.883278e-06 \n", " trade 4.981458e-02 1.014394e-03 \n", " transport 3.801613e-02 2.591267e-03 \n", " other 6.984674e-02 5.676656e-04 \n", "reg6 food 1.119791e-06 6.959613e-04 \n", " mining 9.791972e-04 1.512044e-01 \n", " manufactoring 6.448726e-03 5.043697e-02 \n", " electricity 3.010193e-05 6.787911e-03 \n", " construction 3.829687e-05 2.041605e-03 \n", " trade 1.324385e-04 4.701872e-03 \n", " transport 2.978642e-04 4.032717e-02 \n", " other 2.084267e-04 2.052462e-02 " ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A_mining = mrio.A.loc[:, (slice(None), \"mining\")]\n", "A_mining" ] }, { "cell_type": "markdown", "id": "6b77959c", "metadata": {}, "source": [ "For further information on the pandas multiindex see the [pandas documentation on advanced indexing.](https://pandas.pydata.org/docs/user_guide/advanced.html)\n", "\n", "## Extracting data across extension tables" ] }, { "cell_type": "markdown", "id": "6d77f052", "metadata": {}, "source": [ "Pymrio includes methods for bulk extraction of data across extension tables. These can either work on a specific extension or across all extensions of the system." ] }, { "cell_type": "markdown", "id": "e9f1a370", "metadata": { "lines_to_next_cell": 2 }, "source": [ "### Extracting from a specific extension" ] }, { "cell_type": "markdown", "id": "9ec046cc", "metadata": {}, "source": [ "Here we use use the `extract` method available in the extension object.\n", "This expect a list of rows (index) to extract.\n", "Here we extract some rows from the emission extension table.\n", "To do so, we first define the rows (index) to extract:" ] }, { "cell_type": "code", "execution_count": 43, "id": "debe154e", "metadata": {}, "outputs": [], "source": [ "rows_to_extract = [(\"emission_type1\", \"air\"), (\"emission_type2\", \"water\")]" ] }, { "cell_type": "markdown", "id": "8051fc37", "metadata": {}, "source": [ "We can now use the `extract` method to extract the data, either as a pandas DataFrame" ] }, { "cell_type": "code", "execution_count": 44, "id": "c945af36", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_keys(['F', 'F_Y', 'S', 'S_Y', 'M', 'M_down', 'D_cba', 'D_pba', 'D_imp', 'D_exp', 'unit', 'D_cba_reg', 'D_pba_reg', 'D_imp_reg', 'D_exp_reg', 'D_cba_cap', 'D_pba_cap', 'D_imp_cap', 'D_exp_cap'])" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_extract = mrio.emissions.extract(rows_to_extract, return_type=\"dataframe\")\n", "df_extract.keys()" ] }, { "cell_type": "markdown", "id": "a5aea0ad", "metadata": {}, "source": [ "Or we extract into a new extension object:" ] }, { "cell_type": "code", "execution_count": 45, "id": "8f596735", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Extension Emissions_extracted with parameters: name, F, F_Y, S, S_Y, M, M_down, D_cba, D_pba, D_imp, D_exp, unit, D_cba_reg, D_pba_reg, D_imp_reg, D_exp_reg, D_cba_cap, D_pba_cap, D_imp_cap, D_exp_cap'" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ext_extract = mrio.emissions.extract(rows_to_extract, return_type=\"extension\")\n", "str(ext_extract)" ] }, { "cell_type": "markdown", "id": "66981d62", "metadata": {}, "source": [ "Note that the name of the extension object is now `Emissions_extracted`, based on the name of the original extension object.\n", "To use another name, just pass the name as the `return_type` method." ] }, { "cell_type": "code", "execution_count": 46, "id": "c39cfc64", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Extension new_extension with parameters: name, F, F_Y, S, S_Y, M, M_down, D_cba, D_pba, D_imp, D_exp, unit, D_cba_reg, D_pba_reg, D_imp_reg, D_exp_reg, D_cba_cap, D_pba_cap, D_imp_cap, D_exp_cap'" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new_extension = mrio.emissions.extract(rows_to_extract, return_type=\"new_extension\")\n", "str(new_extension)" ] }, { "cell_type": "markdown", "id": "68f6f3e8", "metadata": {}, "source": [ "Extracting to dataframes is also a convienient\n", "way to convert an extension object to a dictionary:" ] }, { "cell_type": "code", "execution_count": 47, "id": "b23d7415", "metadata": { "lines_to_next_cell": 2 }, "outputs": [ { "data": { "text/plain": [ "dict_keys(['F', 'F_Y', 'S', 'S_Y', 'M', 'M_down', 'D_cba', 'D_pba', 'D_imp', 'D_exp', 'unit', 'D_cba_reg', 'D_pba_reg', 'D_imp_reg', 'D_exp_reg', 'D_cba_cap', 'D_pba_cap', 'D_imp_cap', 'D_exp_cap'])" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_all = mrio.emissions.extract(mrio.emissions.get_rows(), return_type=\"dfs\")\n", "df_all.keys()\n", "\n", "\n", "# The method also allows to only extract some of the accounts:\n", "df_some = mrio.emissions.extract(\n", " mrio.emissions.get_rows(), dataframes=[\"D_cba\", \"D_pba\"], return_type=\"dfs\"\n", ")\n", "df_some.keys()" ] }, { "cell_type": "markdown", "id": "4357fd67", "metadata": {}, "source": [ "### Extracting from all extensions" ] }, { "cell_type": "markdown", "id": "d49af58b", "metadata": {}, "source": [ "We can also extract data from all extensions at once.\n", "This is done using the `extension_extract` method from the pymrio object.\n", "This expect a dict with keys based on the extension names and values as a list of rows (index) to extract." ] }, { "cell_type": "markdown", "id": "db8053ac", "metadata": {}, "source": [ "Lets assume we want to extract value added and all emissions.\n", "We first define the rows (index) to extract:" ] }, { "cell_type": "code", "execution_count": null, "id": "fd730723", "metadata": { "lines_to_next_cell": 2 }, "outputs": [], "source": [ "to_extract = {\n", " \"Factor Inputs\": \"Value Added\",\n", " \"Emissions\": [(\"emission_type1\", \"air\"), (\"emission_type2\", \"water\")],\n", "}" ] }, { "cell_type": "markdown", "id": "0882d1dc", "metadata": {}, "source": [ "And can then use the `extension_extract` method to extract the data, either as a pandas DataFrame,\n", "which returns a dictionary with the extension names as keys" ] }, { "cell_type": "code", "execution_count": null, "id": "dbfe113a", "metadata": {}, "outputs": [], "source": [ "df_extract_all = mrio.extension_extract(to_extract, return_type=\"dataframe\")\n", "df_extract_all.keys()" ] }, { "cell_type": "code", "execution_count": null, "id": "47393c06", "metadata": {}, "outputs": [], "source": [ "df_extract_all[\"Factor Inputs\"].keys()" ] }, { "cell_type": "markdown", "id": "e5fc1452", "metadata": {}, "source": [ "We can also extract into a dictionary of extension objects:" ] }, { "cell_type": "code", "execution_count": null, "id": "b195ef6f", "metadata": {}, "outputs": [], "source": [ "ext_extract_all = mrio.extension_extract(to_extract, return_type=\"extensions\")\n", "ext_extract_all.keys()" ] }, { "cell_type": "code", "execution_count": null, "id": "ee908ec0", "metadata": {}, "outputs": [], "source": [ "str(ext_extract_all[\"Factor Inputs\"])" ] }, { "cell_type": "markdown", "id": "d3eed3a5", "metadata": {}, "source": [ "Or merge the extracted data into a new pymrio Extension object (when passing a new name as return_type):" ] }, { "cell_type": "code", "execution_count": null, "id": "b3690981", "metadata": {}, "outputs": [], "source": [ "ext_new = mrio.extension_extract(to_extract, return_type=\"new_merged_extension\")\n", "str(ext_new)" ] }, { "cell_type": "markdown", "id": "417f397d", "metadata": {}, "source": [ "CONT: Continue with explaining, mention the work with find_all etc" ] }, { "cell_type": "markdown", "id": "a2887bce", "metadata": {}, "source": [ "### Search and extract" ] }, { "cell_type": "markdown", "id": "c5beffce", "metadata": {}, "source": [ "The extract methods can also be used in combination with the [search/explore](./explore.ipynb) methods of pymrio.\n", "This allows to search for specific rows and then extract the data." ] }, { "cell_type": "markdown", "id": "04144a4b", "metadata": {}, "source": [ "For example, to extract all emissions from the air compartment we can use:" ] }, { "cell_type": "code", "execution_count": null, "id": "87303c51", "metadata": {}, "outputs": [], "source": [ "match_air = mrio.extension_match(find_all=\"air\")" ] }, { "cell_type": "markdown", "id": "e50edc2f", "metadata": {}, "source": [ "And then make a new extension object with the extracted data:" ] }, { "cell_type": "code", "execution_count": null, "id": "2cac8d8a", "metadata": {}, "outputs": [], "source": [ "air_emissions = mrio.emissions.extract(match_air, return_type=\"extracted_air_emissions\")\n", "print(air_emissions)" ] }, { "cell_type": "markdown", "id": "9b1fef8b", "metadata": {}, "source": [ "For more information on the search methods see the [explore notebook](./explore.ipynb)." ] }, { "cell_type": "markdown", "id": "4fd51af0-1331-4fa4-9100-1a573377c88f", "metadata": {}, "source": [ "## Extract data from a time series of MRIOs" ] }, { "cell_type": "code", "execution_count": null, "id": "fc01bd6f-c6f3-420f-9207-92d7e669d28e", "metadata": {}, "outputs": [], "source": [] } ], "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.12.2" } }, "nbformat": 4, "nbformat_minor": 5 }