CSV Merging Exercises

The Readings Files

The readings files contain an ID column and two year columns.

For readings1.csv, the year columns are Year 1 and Year 2:

ID,Year 1,Year 2
purple,405,679
blue,589,562
yellow,888,957
orange,508,708
brown,643,1227
green,320,191
red,458,1398
pink,668,1661
black,278,768

For readings2.csv, the year columns are Year 2 and Year 3:

ID,Year 3,Year 4
purple,2094,3326
blue,2892,359
yellow,1038,2737
orange,889,810
brown,2396,1021
green,937,3064
red,2444,3218
pink,465,621
black,559,3243

Same Order Merge

Write a program to merge the two CSV files that have the same first column by combining the columns from the two files (the columns from the first file should be first) and removing the extra ID column from the second file.

Assume the identifiers (the ID values) in the given reading files are the same (same number of rows and same values in each row).

Given this readings1.csv file:

ID,Year 1,Year 2
purple,405,679
blue,589,562
yellow,888,957
orange,508,708
brown,643,1227
green,320,191
red,458,1398
pink,668,1661
black,278,768

And this readings2.csv file:

ID,Year 3,Year 4
purple,2094,3326
blue,2892,359
yellow,1038,2737
orange,889,810
brown,2396,1021
green,937,3064
red,2444,3218
pink,465,621
black,559,3243

The merged readings-combined.csv CSV file should look like this:

ID,Year 1,Year 2,Year 3,Year 4
purple,405,679,2094,3326
blue,589,562,2892,359
yellow,888,957,1038,2737
orange,508,708,889,810
brown,643,1227,2396,1021
green,320,191,937,3064
red,458,1398,2444,3218
pink,668,1661,465,621
black,278,768,559,3243

Different Order Merge

Write a program to merge the two CSV files by the value of their first column by combining the columns from the two files (the columns from the first file should be first).

Assume the identifiers (the ID values) in the given reading files are the same (the values in the first column are the same but are not in the same order).

Given this readings1.csv file:

ID,Year 1,Year 2
orange,508,708
green,320,191
yellow,888,957
red,458,1398
black,278,768
pink,668,1661
brown,643,1227
blue,589,562
purple,405,679

And this readings2.csv file:

ID,Year 3,Year 4
green,937,3064
black,559,3243
blue,2892,359
pink,465,621
red,2444,3218
orange,889,810
brown,2396,1021
yellow,1038,2737
purple,2094,3326

The merged readings-combined.csv CSV file should look similar to this (note that the rows may occur in any order):

ID,Year 1,Year 2,Year 3,Year 4
purple,405,679,2094,3326
blue,589,562,2892,359
yellow,888,957,1038,2737
orange,508,708,889,810
brown,643,1227,2396,1021
green,320,191,937,3064
red,458,1398,2444,3218
pink,668,1661,465,621
black,278,768,559,3243