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   |
|--------------:|---------------:|-----------:|---------------:|---------------:|:-------------|
|      11017881 |             13 |         43 |            369 |             24 | A            |
|      30968800 |             13 |         36 |            578 |             81 | A            |
|      16138071 |             13 |         51 |           6101 |             24 | A            |
|      16048997 |             13 |         52 |           4610 |             24 | A            |
|      24776917 |           6057 |         40 |           8724 |             24 | A            |
|       1592865 |             13 |         74 |           1345 |             23 | A            |
|      35350963 |             13 |         25 |           6345 |             25 | A            |
|      29215355 |             13 |         27 |           5928 |             25 | A            |
|      22963946 |             13 |         36 |           2075 |             24 | A            |
|      23106144 |             13 |         37 |           3029 |             24 | M            |


Table #2
Filename: source_1.csv
Sample Rows in Markdown:
|   COD_IDCONTRA |   COD_PERSONA | FAP_CONTR   |   IMP_CAPDIS |   IMP_CAPINI |   IMP_CAPPEN |
|---------------:|--------------:|:------------|-------------:|-------------:|-------------:|
|      272135692 |       9917522 | 08JAN2016   |            0 |         0    |         0    |
|      372477675 |        364170 | 06MAR2017   |            0 |         8.5  |         8.5  |
|      359485470 |      19736977 | 05SEP2016   |            0 |         0    |         0    |
|      273335816 |       3212053 | 28JAN2016   |         6705 |      6705    |         0    |
|      371963644 |        621994 | 27FEB2017   |         1509 |      1509    |      1471.49 |
|      179110554 |       1861181 | 31DEC2999   |         5100 |      5100    |         0    |
|      374313638 |       7131690 | 31MAR2017   |            0 |       629.88 |       629.88 |
|       65190783 |       8448034 | 30DEC2009   |       135138 |    123258    |    100055    |
|       17768671 |      27832809 | 28MAY2008   |       211000 |    195000    |    144735    |
|      280806626 |      21975295 | 01JUN2016   |            0 |         0    |         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 | XTI_ESTADO   |   QNU_ORDTIT | COD_INTERV   |          COD_IDCONTRA |
|--------------:|:-------------|-------------:|:-------------|----------------------:|
|      20150142 | A            |            1 | TIT          |             144089757 |
|      37892486 | A            |            1 | TIT          |             366542705 |
|      16025774 | A            |            1 | TIT          |              35198784 |
|      21248337 | N            |            1 | TIT          | 100000000000351977472 |
|      21417824 | C            |            1 | TIT          | 100000000000344391680 |
|      34201415 | I            |            1 | TIT          |             357049792 |
|      34892261 | A            |            1 | TIT          |             147129257 |
|      37115124 | A            |            2 | TIT          |             241496635 |
|      32022273 | A            |            1 | PAU          |             195456359 |
|      40360654 | A            |            2 | TIT          |             274380985 |


Target Schema:
| des_zona           |   COD_PERSONA_x |   COD_AREANEGO |
|:-------------------|----------------:|---------------:|
| Z. ARABA - BIZKAIA |      2005227271 |           3003 |
| Z. ASTURIAS        |      2689509658 |           4641 |
| Z. BALEARES        |      2937512279 |           4615 |
| Z. BARCELONA EST   |       958528558 |           1443 |
| Z. BARCELONA NORD  |      1299078076 |           1651 |
| Z. BARCELONA SUD   |      1600116608 |           4225 |
| Z. BESÒS-MARESME   |      1672142965 |           2548 |
| Z. GALICIA CENTRO  |      2412682899 |           3653 |
| Z. GALICIA NORTE   |      2073586674 |           3731 |
| Z. GALICIA SUR     |      2878933003 |           5083 |

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