Task: Transform-by-output-target-schema (source dataset: github-pipelines)

Your task is to create a data transformation pipeline in Python using pandas libraries.

Given Source Tables below, and a Target Schema provided by users that demonstrates how the desired output table should look like schematically, please write Python pandas code that can transform the Source Tables into an output table, that matches the schema/structure given by the Target Schema.  Please note that the data values in the Target Schema are intended to represent the format and layout of the output table, but may not exactly match the actual data generated from the Source Tables when the python code is run on the Source Tables.

Use operations such as merge, groupby, pivot, apply, or any appropriate python or pandas steps. Your code should save the resulting DataFrame to a CSV file named 'output.csv'. Ensure that the code is executable and can be run directly as is, in a Python environment with pandas installed.

Source Tables:


Table #1
Filename: source_0.csv
Sample Rows in Markdown:
|   COD_PERSONA |   COD_AREANEGO |   COD_EDAD |   COD_OFICIPAL |   COD_SEGLOBAL | estado_cli   |
|--------------:|---------------:|-----------:|---------------:|---------------:|:-------------|
|      22146707 |             58 |         59 |           6054 |             23 | I            |
|       9840019 |           6057 |         47 |           9613 |             24 | M            |
|      17119543 |             13 |         83 |           2863 |             22 | A            |
|      12444503 |             13 |         76 |           4137 |             23 | A            |
|      30084804 |           6057 |         43 |           9981 |             24 | I            |
|       5542062 |             13 |         76 |           4051 |             23 | A            |
|      19037559 |           6057 |         58 |           3355 |             80 | I            |
|      33965249 |             13 |         29 |           1292 |             89 | A            |
|      40198089 |             13 |         62 |           5669 |             23 | A            |
|      13190727 |             13 |         45 |           5148 |             22 | A            |


Table #2
Filename: source_1.csv
Sample Rows in Markdown:
|   COD_IDCONTRA |   COD_PERSONA | FAP_CONTR   |   IMP_CAPDIS |   IMP_CAPINI |   IMP_CAPPEN |
|---------------:|--------------:|:------------|-------------:|-------------:|-------------:|
|      368225568 |      20455043 | 05JAN2017   |       1600   |       1600   |         0    |
|      174595357 |       2125320 | 19MAR2009   |     250000   |     250000   |    214162    |
|       82825628 |      32235220 | 28MAY2010   |     206000   |     206000   |    189250    |
|      244824648 |      31402296 | 12DEC2014   |      58320.6 |      58320.6 |     53025.4  |
|      162894558 |      12575083 | 18MAR2013   |      93127.3 |      93127.3 |     88471.4  |
|      283650188 |       6650374 | 21OCT2005   |     110182   |     110182   |     17869.2  |
|       18731780 |       7963421 | 30JAN2002   |      78654.4 |      78654.4 |     23637.5  |
|       20693177 |      20915930 | 13MAY2005   |      93123.8 |      93123.8 |     64336.6  |
|      148066452 |       8969341 | 29FEB2012   |      25380.7 |      25380.7 |      1038.75 |
|      359419852 |      21795465 | 02SEP2016   |      50972.4 |      50972.4 |     48451.1  |


Table #3
Filename: source_2.csv
Sample Rows in Markdown:
|   COD_PERSONA | XTI_ESTADO   |   QNU_ORDTIT | COD_INTERV   |          COD_IDCONTRA |
|--------------:|:-------------|-------------:|:-------------|----------------------:|
|      22928781 | A            |            1 | TIT          |              24381203 |
|      33600429 | A            |            1 | TIT          | 100000000001188118528 |
|      43280071 | A            |            1 | TIT          | 100000000001327874048 |
|      22283121 | C            |            1 | TIT          | 100000000000346308608 |
|      19277207 | A            |            1 | TIT          |             143195414 |
|      17443413 | C            |            1 | TIT          |             359436844 |
|      27988241 | A            |            1 | TIT          |             284835811 |
|      19980306 | A            |            2 | TIT          |             242755922 |
|      29851760 | C            |            1 | ASE          |             257406752 |
|      24292739 | A            |            1 | TIT          |             195686395 |


Table #4
Filename: source_3.csv
Sample Rows in Markdown:
|   COD_OFICI |   COD_NIVELOFIC | des_ofici                         | cod_cbc   | des_cbc   | cod_zona   | des_zona   |   COD_TERRIT | des_territ   | cod_areanego   | des_areanego   |
|------------:|----------------:|:----------------------------------|:----------|:----------|:-----------|:-----------|-------------:|:-------------|:---------------|:---------------|
|           1 |              70 | BANCO BILBAO VIZCAYA ARGENTARIA   |           |           |            |            |            0 |              |                |                |
|        2004 |              40 | AGREGADO FICT. CARTERA DE TITULOS |           |           |            |            |            0 |              |                |                |
|        2221 |              40 | FRANCIA (R)                       |           |           |            |            |            0 |              |                |                |
|        2222 |              40 | ITALIA (R)                        |           |           |            |            |            0 |              |                |                |
|        2223 |              40 | INGLATERRA (R)                    |           |           |            |            |            0 |              |                |                |
|        2224 |              40 | NUEVA YORK (R)                    |           |           |            |            |            0 |              |                |                |
|        2226 |              40 | JAPON (R)                         |           |           |            |            |            0 |              |                |                |
|        2227 |              40 | BELGICA (R)                       |           |           |            |            |            0 |              |                |                |
|        2246 |              40 | CHINA (R)                         |           |           |            |            |            0 |              |                |                |
|        2385 |              40 | AUSTRALIA                         |           |           |            |            |            0 |              |                |                |


Target Schema:
| des_territ           |
|:---------------------|
| D.T. CANARIAS        |
| D.T. NOROESTE        |
| D.T.CENTRO           |
| D.T.ESTE             |
| D.T.NORTE            |
| D.T.SUR              |
| DR CATALUÑA EMPRESAS |
| DR ESTE-EMPRESAS     |
| DT. BARCELONA        |
| DT. CATALUNYA        |

Return only the final code in markdown codeblock format, without any additional text or explanation. 



target: target.csv
train: source_0.csv
source_1.csv
source_2.csv
source_3.csv
test: test_0.csv
test_1.csv
test_2.csv
test_3.csv