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   |
|--------------:|---------------:|-----------:|---------------:|---------------:|:-------------|
|        766431 |           6057 |         56 |            388 |             89 | M            |
|      22936176 |           6057 |         37 |           8645 |             24 | A            |
|         10327 |             13 |         59 |           1600 |             23 | A            |
|      21198531 |           4747 |         57 |           5777 |             22 | A            |
|      34462128 |           4747 |         34 |           1042 |             24 | A            |
|       1648399 |           6057 |         39 |           3368 |             83 | I            |
|      27318963 |           6057 |         77 |           1025 |             23 | I            |
|       3804560 |             13 |         82 |           6809 |             23 | I            |
|       6815056 |             13 |         33 |           3352 |             24 | A            |
|      19143036 |           6057 |         47 |           8645 |             24 | A            |


Table #2
Filename: source_1.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 #3
Filename: source_2.csv
Sample Rows in Markdown:
|   COD_PERSONA | XTI_ESTADO   |   QNU_ORDTIT | COD_INTERV   |          COD_IDCONTRA |
|--------------:|:-------------|-------------:|:-------------|----------------------:|
|      28888904 | C            |            1 | TIT          | 100000000000514949120 |
|      17090363 | A            |            1 | ASE          |             144816735 |
|      23063566 | C            |            1 | TIT          | 100000000000343179264 |
|      18384010 | C            |            1 | TIT          |              26254265 |
|      19930494 | A            |            2 | TIT          | 100000000000349552640 |
|      28166962 | A            |            2 | TIT          |             193719874 |
|      45660732 | A            |            2 | TIT          |             283125678 |
|      16247605 | C            |            1 | TIT          | 100000000000455344128 |
|      44838111 | A            |            1 | TIT          |             320140343 |
|      18989194 | A            |            1 | TIT          |               1481791 |


Table #4
Filename: source_3.csv
Sample Rows in Markdown:
|   COD_IDCONTRA |   COD_PERSONA | FAP_CONTR   |   IMP_CAPDIS |   IMP_CAPINI |   IMP_CAPPEN |
|---------------:|--------------:|:------------|-------------:|-------------:|-------------:|
|      363799860 |       6167983 | 31OCT2016   |         0    |       339.36 |       309.98 |
|       16804392 |       1500144 | 31DEC2004   |    122216    |    122216    |     81587.2  |
|      261187179 |      24730962 | 28JUL2015   |     12793.1  |     12793.1  |      8530.79 |
|      272060467 |         28522 | 12JAN2016   |      7991.53 |      7991.53 |         0    |
|      258553940 |      34170924 | 18JUN2015   |      5125    |      5125    |         0    |
|      364035973 |       6521972 | 03NOV2016   |      1204    |      1204    |         0    |
|      277999819 |       3527271 | 14APR2016   |     30000    |     30000    |     27344.3  |
|       19544951 |      18789091 | 15JAN2008   |    110000    |    110000    |     24477.2  |
|      267107750 |       4662621 | 03NOV2015   |         0    |         0    |         0    |
|       18111465 |        576480 | 26MAY2004   |         0    |    186170    |         0    |


Target Schema:
| FAP_CONTR   | estado_cli   |   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 | COD_INTERV   |
|:------------|:-------------|--------------:|---------------:|-----------:|---------------:|---------------:|------------:|----------------:|:---------------------------|----------:|:---------------------------------|-----------:|:--------------------|-------------:|:--------------|---------------:|:----------------|----------------------:|----------------:|-------------:|-------------:|-------------:|----------------:|:-------------|-------------:|:-------------|
| 01APR1984   | A            |       2684514 |             13 |         92 |            382 |             23 |         382 |              10 | ZAFRA-ALVAREZ CHAMORRO     |      8993 | CBC-ZAFRA-ALVAREZ CHAMOR         |       9826 | Z.EXTREMADURA       |           72 | D.T.SUR       |             13 | BANCA COMERCIAL | 100000000000348422144 |         2684514 |      13023.9 |      13023.9 |         0    |         2684514 | C            |            1 | TIT          |
| 01APR1986   | A            |       2441540 |             13 |         61 |           1002 |             23 |        1002 |              10 | BARCELONA-AUSIAS MARC      |      9274 | CBC BARCELONA AUSIAS MARC        |       9897 | Z. BARCELONA SUD    |         8302 | DT. BARCELONA |             13 | BANCA COMERCIAL |              24563806 |         2441540 |      15241.7 |      12621.2 |         0    |         2441540 | C            |            1 | TIT          |
| 01APR2004   | A            |      14841299 |             13 |         48 |           8134 |             24 |        8134 |              10 | BADALONA - CENTRE          |      9589 | CBC-BADALONA-F.LAYRET            |       8962 | Z. BESÒS-MARESME    |         8302 | DT. BARCELONA |             13 | BANCA COMERCIAL |             168318027 |        14841299 |     128000   |     128000   |     86522.9  |        14841299 | A            |            1 | TIT          |
| 01APR2005   | A            |       8796536 |             13 |         57 |            262 |             24 |         262 |              10 | BADALONA-FRANCESC LAYRET   |      9589 | CBC-BADALONA-F.LAYRET            |       8962 | Z. BESÒS-MARESME    |         8302 | DT. BARCELONA |             13 | BANCA COMERCIAL |              16860420 |         8796536 |     204500   |     204500   |    147997    |         8796536 | A            |            1 | TIT          |
| 01APR2008   | A            |      11203265 |             13 |         48 |            116 |             81 |         116 |              10 | DENIA-MARQUES DE CAMPO, 29 |      9440 | CBC-DENIA-JAVEA                  |       9884 | Z. LEVANTE          |         1964 | D.T.ESTE      |             13 | BANCA COMERCIAL |             283590224 |        11203265 |     114670   |     114670   |     94683    |        11203265 | A            |            1 | TIT          |
| 01APR2009   | A            |        683974 |             13 |         34 |            625 |             24 |         625 |              10 | LUGO-REINA                 |      9355 | CBC-LUGO                         |       9842 | Z. GALICIA CENTRO   |         1525 | D.T. NOROESTE |             13 | BANCA COMERCIAL |              24300750 |          683974 |      89428.8 |      89428.8 |     67492.2  |          683974 | A            |            1 | TIT          |
| 01APR2010   | A            |       9584799 |             13 |         60 |           3032 |             23 |        3032 |              10 | BCN - MERCAT SANT ANTONI   |      9318 | CBC BARCELONA MERCAT SANT ANTONI |       9897 | Z. BARCELONA SUD    |         8302 | DT. BARCELONA |             13 | BANCA COMERCIAL |              80167178 |         9584799 |      24322.2 |      24322.2 |      4596.1  |         9584799 | A            |            1 | TIT          |
| 01APR2014   | A            |      13377186 |             13 |         51 |           2305 |             81 |        2305 |              10 | SALAMANCA-TORO, 67         |      9422 | CBC-SALAMANCA-TORO 67            |       9847 | Z.VALLADOLID        |         1525 | D.T. NOROESTE |             13 | BANCA COMERCIAL |             234454913 |        13377186 |       9950   |       9950   |      6325.87 |        13377186 | A            |            1 | TIT          |
| 01APR2015   | A            |      33725331 |             13 |         37 |           4170 |             24 |        4170 |              10 | MATARO - CAMI RAL          |      9639 | CBC MATARÒ CAMÍ RAL              |       8962 | Z. BESÒS-MARESME    |         8302 | DT. BARCELONA |             13 | BANCA COMERCIAL | 100000000001195245568 |        33725331 |      98577.3 |      98577.3 |     93709.1  |          260740 | A            |            1 | TIT          |
| 01APR2016   | A            |      11888420 |             13 |         39 |           6755 |             24 |        6755 |              10 | VILLARRUBIA DE LOS OJOS    |      6688 | CBC-BOLAÑOS DE CALATRAVA         |       9801 | Z.MANCHA OCCIDENTAL |           51 | D.T.CENTRO    |             13 | BANCA COMERCIAL | 100000000001229848576 |        11888420 |          0   |          0   |         0    |          118488 | A            |            1 | TIT          |

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