Merge and aggregate datasets



Task

Merge and aggregate two datasets as provided in   .csv   files into a new resulting dataset.

Use the appropriate methods and data structures depending on the programming language.

Use the most common libraries only when built-in functionality is not sufficient.



Note

Either load the data from the   .csv   files or create the required data structures hard-coded.


patients.csv   file contents:

PATIENT_ID,LASTNAME
1001,Hopper
4004,Wirth
3003,Kemeny
2002,Gosling
5005,Kurtz


visits.csv   file contents:

PATIENT_ID,VISIT_DATE,SCORE
2002,2020-09-10,6.8
1001,2020-09-17,5.5
4004,2020-09-24,8.4
2002,2020-10-08,
1001,,6.6
3003,2020-11-12,
4004,2020-11-05,7.0
1001,2020-11-19,5.3



Create a resulting dataset in-memory or output it to screen or file, whichever is appropriate for the programming language at hand.

Merge and group per patient id and last name,   get the maximum visit date,   and get the sum and average of the scores per patient to get the resulting dataset.



Note that the visit date is purposefully provided as ISO format,   so that it could also be processed as text and sorted alphabetically to determine the maximum date.

| PATIENT_ID | LASTNAME | LAST_VISIT |  SCORE_SUM | SCORE_AVG |
| 1001       |  Hopper  | 2020-11-19 |  17.4      | 5.80      |
| 2002       |  Gosling | 2020-10-08 |   6.8      | 6.80      |
| 3003       |  Kemeny  | 2020-11-12 |            |           |
| 4004       |  Wirth   | 2020-11-05 |  15.4      | 7.70      |
| 5005       |  Kurtz   |            |            |           |



Note

This task is aimed in particular at programming languages that are used in data science and data processing, such as F#, Python, R, SPSS, MATLAB etc.



Related tasks

 CSV data manipulation
 CSV to HTML translation
 Read entire file
 Read a file line by line


BEGIN; 
  TYPE Date UNION; 
  TYPE DateValid IS {Date POSSREP {year INTEGER, month INTEGER, day INTEGER}};
  TYPE DateNone IS {Date POSSREP {}};
  TYPE DateUnknown IS {Date POSSREP {}};
END;
 
VAR patient REAL RELATION {id INT, lastname CHAR} KEY {id};
 
INSERT patient RELATION
{TUPLE {id 1001, lastname 'Hopper'},
 TUPLE {id 4004, lastname 'Wirth'},
 TUPLE {id 3003, lastname 'Kemeny'},
 TUPLE {id 2002, lastname 'Gosling'},
 TUPLE {id 5005, lastname 'Kurtz'}
 };
 
VAR visit REAL RELATION {id INT, date Date, score RATIONAL} KEY {id, date};
 
INSERT visit RELATION
{
TUPLE {id 2002, date DateValid(2020,09,10), score 6.8},
TUPLE {id 1001, date DateValid(2020,09,17), score 5.5},
TUPLE {id 4004, date DateValid(2020,09,24), score 8.4},
TUPLE {id 2002, date DateValid(2020,10,08), score NAN},
TUPLE {id 1001, date DateNone(), score 6.6},
TUPLE {id 3003, date DateValid(2020,11,12), score NAN},
TUPLE {id 4004, date DateValid(2020,11,05), score 7.0},
TUPLE {id 1001, date DateValid(2020,11,19), score 5.3}
};
 
((SUMMARIZE (visit WHERE score>0.0) BY {id}: {sumscore := SUM(score), avgscore := AVG(score)}) UNION
 (EXTEND (patient {id} MINUS ((visit WHERE score>0.0) {id})): {sumscore := NaN, avgscore := NaN})) JOIN
    (SUMMARIZE visit BY {id}: {maxdate := MAX(date)} UNION
     (EXTEND (patient {id} MINUS (visit {id})): {maxdate := DateUnknown()})) JOIN
    patient