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 | XTI_ESTADO   |   QNU_ORDTIT | COD_INTERV   |          COD_IDCONTRA |
|--------------:|:-------------|-------------:|:-------------|----------------------:|
|      45807352 | A            |            1 | TIT          |             295454160 |
|      16125538 | C            |            1 | TIT          | 100000000001030340608 |
|      24440992 | C            |            1 | TIT          |             286287110 |
|      37023105 | A            |            1 | TIT          |             177652616 |
|      33801457 | A            |            1 | REP          |             310055060 |
|      18993480 | A            |            1 | TIT          |              19652747 |
|      28692583 | A            |            1 | TIT          |             196071225 |
|      19575921 | C            |            1 | TIT          |             287158421 |
|      19602165 | C            |            1 | TIT          |             286927835 |
|      34984281 | I            |            1 | TIT          |             237063808 |


Table #2
Filename: source_1.csv
Sample Rows in Markdown:
|   COD_IDCONTRA |   COD_PERSONA | FAP_CONTR   |   IMP_CAPDIS |   IMP_CAPINI |   IMP_CAPPEN |
|---------------:|--------------:|:------------|-------------:|-------------:|-------------:|
|       21811618 |      23256128 | 15OCT2004   |     176000   |     176000   |    111114    |
|      264988912 |       8485097 | 02OCT2015   |      10318.5 |      10318.5 |      8728.28 |
|      256847910 |       4935324 | 10JUN2015   |      50000   |      50000   |     46099    |
|       88705708 |      18772953 | 29JUN2010   |     175615   |     117615   |    151178    |
|      370393924 |      27091679 | 03FEB2017   |          0   |        247.3 |       164.87 |
|      359530127 |      11888420 | 05SEP2016   |          0   |       1000   |       868.23 |
|       20984359 |       8072229 | 14MAY2004   |     238981   |     238981   |    156626    |
|      198763598 |       4456057 | 14NOV2013   |      14160.1 |      14160.1 |      7445.83 |
|      273662572 |      33309985 | 03FEB2016   |      17042   |      17042   |     14998.2  |
|      370964177 |      29404855 | 13FEB2017   |          0   |        300   |         0    |


Table #3
Filename: source_2.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 |              |                |                |


Table #4
Filename: source_3.csv
Sample Rows in Markdown:
|   COD_PERSONA |   COD_AREANEGO |   COD_EDAD |   COD_OFICIPAL |   COD_SEGLOBAL | estado_cli   |
|--------------:|---------------:|-----------:|---------------:|---------------:|:-------------|
|      16185045 |             16 |         45 |           2874 |             24 | A            |
|      31727427 |             13 |         31 |           1054 |             24 | A            |
|      29432787 |             13 |         22 |            386 |             25 | A            |
|      31457817 |             16 |         28 |           3378 |             25 | A            |
|       2859039 |             13 |         91 |           1830 |             22 | A            |
|      28763179 |           6057 |         49 |           9981 |             24 | I            |
|      34397820 |             13 |         32 |           7320 |             24 | A            |
|      30796686 |           6057 |         51 |           9981 |             24 | I            |
|      20352859 |             13 |         45 |           4462 |             24 | M            |
|      10947028 |             13 |         80 |           7212 |             23 | A            |


Target Schema:
| FAP_CONTR   | estado_cli   | COD_INTERV   |   COD_PERSONA |   COD_AREANEGO |   COD_EDAD |   COD_OFICIPAL |   COD_SEGLOBAL |   COD_OFICI |   COD_NIVELOFIC |   des_ofici |   cod_cbc |   des_cbc |   cod_zona |   des_zona |   COD_TERRIT |   des_territ |   cod_areanego |   des_areanego |   COD_IDCONTRA |   COD_PERSONA_x |   IMP_CAPDIS |   IMP_CAPINI |   IMP_CAPPEN |   COD_PERSONA_y |   XTI_ESTADO |   QNU_ORDTIT |
|:------------|:-------------|:-------------|--------------:|---------------:|-----------:|---------------:|---------------:|------------:|----------------:|------------:|----------:|----------:|-----------:|-----------:|-------------:|-------------:|---------------:|---------------:|---------------:|----------------:|-------------:|-------------:|-------------:|----------------:|-------------:|-------------:|
| 01APR1984   | A            | TIT          |             1 |              1 |          1 |              1 |              1 |           1 |               1 |           1 |         1 |         1 |          1 |          1 |            1 |            1 |              1 |              1 |              1 |               1 |            1 |            1 |            1 |               1 |            1 |            1 |
| 01APR1986   | A            | TIT          |             1 |              1 |          1 |              1 |              1 |           1 |               1 |           1 |         1 |         1 |          1 |          1 |            1 |            1 |              1 |              1 |              1 |               1 |            1 |            1 |            1 |               1 |            1 |            1 |
| 01APR2004   | A            | TIT          |             3 |              3 |          3 |              3 |              3 |           3 |               3 |           3 |         3 |         3 |          3 |          3 |            3 |            3 |              3 |              3 |              3 |               3 |            3 |            3 |            3 |               3 |            3 |            3 |
| 01APR2005   | A            | TIT          |             2 |              2 |          2 |              2 |              2 |           2 |               2 |           2 |         2 |         2 |          2 |          2 |            2 |            2 |              2 |              2 |              2 |               2 |            2 |            2 |            2 |               2 |            2 |            2 |
| 01APR2008   | A            | TIT          |             1 |              1 |          1 |              1 |              1 |           1 |               1 |           1 |         1 |         1 |          1 |          1 |            1 |            1 |              1 |              1 |              1 |               1 |            1 |            1 |            1 |               1 |            1 |            1 |
| 01APR2009   | A            | TIT          |             1 |              1 |          1 |              1 |              1 |           1 |               1 |           1 |         1 |         1 |          1 |          1 |            1 |            1 |              1 |              1 |              1 |               1 |            1 |            1 |            1 |               1 |            1 |            1 |
| 01APR2010   | A            | TIT          |             1 |              1 |          1 |              1 |              1 |           1 |               1 |           1 |         1 |         1 |          1 |          1 |            1 |            1 |              1 |              1 |              1 |               1 |            1 |            1 |            1 |               1 |            1 |            1 |
| 01APR2014   | A            | TIT          |             1 |              1 |          1 |              1 |              1 |           1 |               1 |           1 |         1 |         1 |          1 |          1 |            1 |            1 |              1 |              1 |              1 |               1 |            1 |            1 |            1 |               1 |            1 |            1 |
| 01APR2015   | A            | PAU          |             2 |              2 |          2 |              2 |              2 |           2 |               2 |           2 |         2 |         2 |          2 |          2 |            2 |            2 |              2 |              2 |              2 |               2 |            2 |            2 |            2 |               2 |            2 |            2 |
| 01APR2015   | A            | REP          |             1 |              1 |          1 |              1 |              1 |           1 |               1 |           1 |         1 |         1 |          1 |          1 |            1 |            1 |              1 |              1 |              1 |               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