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   |
|--------------:|---------------:|-----------:|---------------:|---------------:|:-------------|
|       9580991 |             13 |         82 |            120 |             23 | M            |
|       5070701 |           6057 |         54 |           9981 |             24 | I            |
|       5377317 |             13 |         53 |            512 |             24 | A            |
|      31215926 |           6057 |         32 |           8248 |             24 | I            |
|       3141191 |             13 |         78 |           1307 |             21 | A            |
|       4563149 |             13 |         53 |           2025 |             13 | A            |
|      16291851 |             13 |         56 |            870 |             24 | A            |
|      10736092 |             13 |         35 |           2645 |             24 | A            |
|      27708251 |             13 |         30 |            298 |             24 | A            |
|      37678543 |             13 |         95 |           3962 |             23 | A            |


Table #2
Filename: source_1.csv
Sample Rows in Markdown:
|   COD_IDCONTRA |   COD_PERSONA | FAP_CONTR   |   IMP_CAPDIS |   IMP_CAPINI |   IMP_CAPPEN |
|---------------:|--------------:|:------------|-------------:|-------------:|-------------:|
|       16953121 |      20310828 | 09APR1999   |    116111    |     66111.3  |         0    |
|      263349537 |       8489138 | 07SEP2015   |      6436.84 |      6436.84 |      4860.55 |
|       64778690 |        270384 | 30NOV2009   |    250000    |    250000    |    200014    |
|       21351542 |      23513637 | 26MAY2004   |     33000    |     33000    |         0    |
|      370106599 |      29509887 | 31JAN2017   |         0    |        77.43 |        52    |
|       19176438 |      23441309 | 05FEB2002   |     45540    |     45540    |     27962.1  |
|      360536802 |      19166041 | 21SEP2016   |         0    |         0    |         0    |
|      356108386 |      28422785 | 04JUL2016   |         0    |         0    |         0    |
|       60956834 |      22037237 | 10NOV2009   |     19764.5  |     19764.5  |         0    |
|      358257459 |      13853511 | 11AUG2016   |      7509.65 |      7509.65 |      6761.57 |


Table #3
Filename: source_2.csv
Sample Rows in Markdown:
|   COD_PERSONA | XTI_ESTADO   |   QNU_ORDTIT | COD_INTERV   |          COD_IDCONTRA |
|--------------:|:-------------|-------------:|:-------------|----------------------:|
|      27151818 | A            |            1 | TIT          | 100000000001325793280 |
|      40082922 | A            |            1 | TIT          |             269288895 |
|      31040639 | I            |            1 | TIT          |             262558394 |
|      45061753 | A            |            1 | TIT          |             307383843 |
|      33154130 | A            |            1 | TIT          |             355289157 |
|      22660540 | A            |            1 | TIT          |             274549001 |
|      38115312 | A            |            1 | TIT          |             179228391 |
|      22236517 | A            |            1 | TIT          |               1190235 |
|      35439067 | C            |            1 | TIT          |             363244776 |
|      21207586 | A            |            1 | TIT          |              19978650 |


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   |   des_ofici |   cod_cbc |   des_cbc |   cod_zona |   des_zona |   COD_TERRIT |   des_territ |
|:-------------|:-------------|------------:|----------:|----------:|-----------:|-----------:|-------------:|-------------:|
| ASE          | A            |         152 |       152 |       152 |        152 |        152 |          152 |          152 |
| BEN          | A            |           8 |         8 |         8 |          8 |          8 |            8 |            8 |
| GAR          | A            |         164 |       164 |       164 |        164 |        164 |          164 |          164 |
| GRE          | A            |           2 |         2 |         2 |          2 |          2 |            2 |            2 |
| PAU          | A            |         147 |       146 |       146 |        147 |        147 |          147 |          147 |
| REP          | A            |          98 |        98 |        98 |         98 |         98 |           98 |           98 |
| TIT          | A            |       15417 |     15370 |     15370 |      15417 |      15417 |        15417 |        15417 |
| TIT          | I            |           5 |         5 |         5 |          5 |          5 |            5 |            5 |
| USU          | A            |           1 |         1 |         1 |          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