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_IDCONTRA |   COD_PERSONA | FAP_CONTR   |   IMP_CAPDIS |   IMP_CAPINI |   IMP_CAPPEN |
|---------------:|--------------:|:------------|-------------:|-------------:|-------------:|
|      283353113 |       1079628 | 11MAY2012   |      3000    |      3000    |      2738.87 |
|      272903883 |      22485616 | 21JAN2016   |      5314.77 |      5314.77 |      3979.34 |
|      360123240 |      27599388 | 14SEP2016   |         0    |       199.2  |       103.27 |
|      234807348 |      16076675 | 18JUL2014   |     84634.1  |     84634.1  |     79534.2  |
|       21521325 |      16575890 | 17DEC2004   |    236700    |    236700    |    121018    |
|       19908277 |      29476981 | 03JUN2009   |    170539    |    170539    |    139129    |
|       21562003 |      22167571 | 10MAR2008   |    167697    |    167697    |    106896    |
|      339602947 |      45662537 | 02FEB2006   |    222000    |    222000    |    160957    |
|      248781921 |      31593224 | 19DEC2014   |      3000    |      3000    |       953.18 |
|      170582922 |       4742536 | 06AUG2003   |     75127    |     75127    |     48353.9  |


Table #2
Filename: source_1.csv
Sample Rows in Markdown:
|   COD_PERSONA | XTI_ESTADO   |   QNU_ORDTIT | COD_INTERV   |          COD_IDCONTRA |
|--------------:|:-------------|-------------:|:-------------|----------------------:|
|      31933902 | A            |            1 | TIT          |             187014466 |
|      18705580 | N            |            1 | TIT          | 100000000000353402880 |
|      32241979 | I            |            1 | TIT          |             265838037 |
|      34663324 | A            |            1 | TIT          |             144709531 |
|      38636370 | A            |            3 | GAR          |             174323348 |
|      42722390 | A            |            2 | TIT          |             304941696 |
|      37949461 | I            |            1 | TIT          |             371418186 |
|      30752101 | I            |            1 | TIT          |             194831764 |
|      37673815 |              |            1 | TIT          |             366388001 |
|      24386609 |              |            2 | GAR          |             369482155 |


Table #3
Filename: source_2.csv
Sample Rows in Markdown:
|   COD_PERSONA |   COD_AREANEGO |   COD_EDAD |   COD_OFICIPAL |   COD_SEGLOBAL | estado_cli   |
|--------------:|---------------:|-----------:|---------------:|---------------:|:-------------|
|      16751309 |             13 |         80 |           7911 |             21 | A            |
|      29839528 |             13 |         53 |            116 |             24 | A            |
|      33477203 |           6057 |         54 |           9534 |             24 | I            |
|      20784484 |             13 |         36 |           7518 |             24 | A            |
|       5667733 |           6057 |         56 |           8248 |             24 | I            |
|      28078041 |             13 |         24 |           3350 |             25 | A            |
|      18903200 |             13 |         49 |           1256 |             22 | A            |
|      29558430 |             13 |         48 |            288 |             14 | A            |
|      16516803 |             13 |         71 |            124 |             23 | A            |
|        888699 |           6057 |         40 |           9894 |             24 | A            |


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:
| COD_INTERV   | estado_cli   |   COD_OFICIPAL |   COD_SEGLOBAL |   COD_OFICI |   COD_NIVELOFIC |
|:-------------|:-------------|---------------:|---------------:|------------:|----------------:|
| ASE          | A            |            152 |            152 |         152 |             152 |
| BEN          | A            |              8 |              8 |           8 |               8 |
| GAR          | A            |            164 |            164 |         164 |             164 |
| GRE          | A            |              2 |              2 |           2 |               2 |
| PAU          | A            |            147 |            147 |         147 |             147 |
| REP          | A            |             98 |             98 |          98 |              98 |
| TIT          | A            |          15417 |          15417 |       15417 |           15417 |
| TIT          | I            |              5 |              5 |           5 |               5 |
| USU          | A            |              1 |              1 |           1 |               1 |

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