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:
|   SubjectId | Split   | Subject   |   PA |   AB |   H |   TB |   BB |   SF |   HBP |
|------------:|:--------|:----------|-----:|-----:|----:|-----:|-----:|-----:|------:|
|      542436 | vs RHP  | HitterId  |   26 |   25 |   5 |    5 |    1 |    0 |     0 |
|      542455 | vs RHP  | HitterId  |    4 |    4 |   0 |    0 |    0 |    0 |     0 |
|      542513 | vs LHP  | HitterId  |    1 |    1 |   0 |    0 |    0 |    0 |     0 |
|      542513 | vs RHP  | HitterId  |    8 |    8 |   2 |    5 |    0 |    0 |     0 |
|      542583 | vs LHP  | HitterId  |    1 |    1 |   0 |    0 |    0 |    0 |     0 |
|      542583 | vs RHP  | HitterId  |    4 |    4 |   0 |    0 |    0 |    0 |     0 |
|      542881 | vs RHP  | HitterId  |    5 |    2 |   0 |    0 |    2 |    0 |     0 |
|      542908 | vs LHP  | HitterId  |    9 |    9 |   1 |    2 |    0 |    0 |     0 |
|      542908 | vs RHP  | HitterId  |   11 |   11 |   2 |    3 |    0 |    0 |     0 |
|      542979 | vs LHP  | HitterId  |    3 |    3 |   1 |    4 |    0 |    0 |     0 |


Table #2
Filename: source_1.csv
Sample Rows in Markdown:
|   SubjectId | Split   | Subject       |   PA |   AB |   H |   TB |   BB |   SF |   HBP |
|------------:|:--------|:--------------|-----:|-----:|----:|-----:|-----:|-----:|------:|
|         108 | vs LHH  | PitcherTeamId |  175 |  161 |  36 |   58 |   12 |    1 |     1 |
|         108 | vs RHH  | PitcherTeamId |  445 |  393 | 107 |  155 |   42 |    5 |     5 |
|         109 | vs LHH  | PitcherTeamId |  298 |  262 |  60 |   95 |   33 |    1 |     1 |
|         109 | vs RHH  | PitcherTeamId |  325 |  290 |  65 |  115 |   25 |    4 |     4 |
|         110 | vs LHH  | PitcherTeamId |  228 |  202 |  45 |   75 |   19 |    1 |     3 |
|         110 | vs RHH  | PitcherTeamId |  404 |  367 |  87 |  130 |   28 |    2 |     5 |
|         111 | vs LHH  | PitcherTeamId |  250 |  218 |  49 |   71 |   23 |    1 |     4 |
|         111 | vs RHH  | PitcherTeamId |  385 |  348 |  86 |  140 |   30 |    4 |     2 |
|         112 | vs LHH  | PitcherTeamId |  180 |  162 |  38 |   64 |   13 |    0 |     5 |
|         112 | vs RHH  | PitcherTeamId |  433 |  391 |  90 |  148 |   34 |    2 |     3 |


Table #3
Filename: source_2.csv
Sample Rows in Markdown:
|   SubjectId | Split   | Subject   |   PA |   AB |   H |   TB |   BB |   SF |   HBP |
|------------:|:--------|:----------|-----:|-----:|----:|-----:|-----:|-----:|------:|
|      112526 | vs LHH  | PitcherId |   48 |   45 |  13 |   22 |    2 |    0 |     0 |
|      112526 | vs RHH  | PitcherId |   37 |   37 |   9 |   13 |    0 |    0 |     0 |
|      150274 | vs LHH  | PitcherId |    3 |    3 |   0 |    0 |    0 |    0 |     0 |
|      150274 | vs RHH  | PitcherId |    2 |    2 |   0 |    0 |    0 |    0 |     0 |
|      276351 | vs LHH  | PitcherId |   16 |   13 |   4 |   11 |    3 |    0 |     0 |
|      276351 | vs RHH  | PitcherId |   16 |   14 |   6 |   13 |    2 |    0 |     0 |
|      276542 | vs LHH  | PitcherId |   11 |   10 |   3 |    4 |    1 |    0 |     0 |
|      276542 | vs RHH  | PitcherId |    8 |    8 |   1 |    1 |    0 |    0 |     0 |
|      279571 | vs LHH  | PitcherId |    3 |    3 |   1 |    1 |    0 |    0 |     0 |
|      279571 | vs RHH  | PitcherId |    8 |    8 |   1 |    2 |    0 |    0 |     0 |


Table #4
Filename: source_3.csv
Sample Rows in Markdown:
|   SubjectId | Split   | Subject      |   PA |   AB |   H |   TB |   BB |   SF |   HBP |
|------------:|:--------|:-------------|-----:|-----:|----:|-----:|-----:|-----:|------:|
|         108 | vs LHP  | HitterTeamId |  176 |  163 |  37 |   63 |   11 |    0 |     2 |
|         108 | vs RHP  | HitterTeamId |  459 |  401 |  99 |  147 |   50 |    3 |     3 |
|         109 | vs LHP  | HitterTeamId |  158 |  144 |  36 |   53 |   12 |    1 |     0 |
|         109 | vs RHP  | HitterTeamId |  463 |  418 |  92 |  170 |   34 |    2 |     4 |
|         110 | vs LHP  | HitterTeamId |  159 |  143 |  36 |   59 |   15 |    1 |     0 |
|         110 | vs RHP  | HitterTeamId |  473 |  424 |  95 |  155 |   33 |    6 |     6 |
|         111 | vs LHP  | HitterTeamId |  147 |  130 |  30 |   51 |   15 |    0 |     0 |
|         111 | vs RHP  | HitterTeamId |  491 |  434 | 109 |  176 |   50 |    2 |     4 |
|         112 | vs LHP  | HitterTeamId |   85 |   75 |  23 |   33 |   10 |    0 |     0 |
|         112 | vs RHP  | HitterTeamId |  570 |  495 | 134 |  223 |   61 |    2 |    10 |


Target Schema:
| Split   |   SubjectId |   Subject |   PA |   AB |   H |   TB |   BB |   SF |   HBP |
|:--------|------------:|----------:|-----:|-----:|----:|-----:|-----:|-----:|------:|
| vs LHH  |         557 |       557 |  557 |  557 | 557 |  557 |  557 |  557 |   557 |
| vs LHP  |         301 |       301 |  301 |  301 | 301 |  301 |  301 |  301 |   301 |
| vs RHH  |         559 |       559 |  559 |  559 | 559 |  559 |  559 |  559 |   559 |
| vs RHP  |         326 |       326 |  326 |  326 | 326 |  326 |  326 |  326 |   326 |

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