NAME
data-prepare - prepare CSV data for automatic processing
SYNOPSIS
data-prepare [-f config] [-v] [[-u|-a|-sn colnum|-sk key] file...]
DESCRIPTION
Uses Data::Prepare to process the specified CSV files to make them suitable for automatic processing (such as data science applications). It will first delete columns specified in chop_cols
, then do the merge
operations, then the chop_lines
operations (in that order so that all lines are available for merge
purposes).
If the flags that take files are given, the config file is not read and no operations executed.
Please note this program overwrites the data files with updated data. Your workflow needs to take this into account by e.g. copying the data into place before calling this program. Use of a version-control system such as Git is also recommended.
OPTIONS
- -u file...
-
For each given file (the config is ignored), prints out any non-unique column values in the first row ("column headers"), with the number of times they occur. Use this to see if further merge/modify operations are needed on column headers in order to achieve uniquely-named columns.
- -a file...
-
Print for each file, a sequence of numbers. Each number is the count of non-blank entries in that column (from left to right). This helps you spot columns with few or no entries that you may wish to "chop".
- -s colnum file...
-
Print for each file, the zero-based-number-th column ("slice") from that file.
- -v
-
Turn on verbose mode.
- -f config
-
Use the given YAML-formatted config file rather than the default of data-prepare-conf.yml. See below for format.
CONFIGURATION FILE FORMAT
This is in YAML format. An example is given below (included in the distribution, together with the applicable CSV file, in the examples directory):
---
chop_cols:
examples/CoreHouseholdIndicators.csv: [0, 2, 4, 7, 10, 13, 16, 19, 21, 22, 23, 25, 26, 29, 32]
chop_lines:
examples/CoreHouseholdIndicators.csv: [0, 0, 0, -1, -1, -1, -1, -1]
merge:
- files:
- examples/CoreHouseholdIndicators.csv
spec:
- do:
- overwrite
from: up
fromspec: lastnonblank
line: 2
matchto: HH
to: self
- do:
- prepend
- ' '
from: self
line: 2
matchfrom: .
to: down
- do:
- prepend
- /
from: self
fromspec: left
line: 3
matchto: Year
to: self
- do:
- overwrite
from: self
fromspec: literal:Country
line: 3
to: self
tospec: index:0
This turns the first three lines of CSV excerpted from the supplied example data (spaces inserted for alignment reasons only):
,Proportion of households with, , ,
,(HH1) ,Year ,(HH2),Year
,Radio ,of data,TV ,of data
Belize,58.7 ,2019 ,78.7 ,2019
into the following. Note that the first two lines will still be present (not shown), possibly modified, so you will need your chop_lines to remove them. The columns of the third line are shown, one per line, for readability:
Country,
Proportion of households with Radio,
Proportion of households with Radio/Year of data,
Proportion of households with TV,
Proportion of households with TV/Year of data
This achieves a single row of column-headings, with each column-heading being unique, and sufficiently meaningful.
WORKFLOW
This is one workflow, using the supplied example config, and recreating the supplied example data by re-downloading it from the International Telecommunication Union (ITU):
mkdir -p xlsx examples
# localc --convert-to xlsx:"Calc MS Excel 2007 XML" --outdir xlsx file.xls # convert other spreadsheet format
wget https://www.itu.int/en/ITU-D/Statistics/Documents/statistics/2020/CoreHouseholdIndicators.xlsx -P xlsx
# after: pip3 install --user xlsx2csv
~/.local/bin/xlsx2csv -i -a xlsx/CoreHouseholdIndicators.xlsx examples
data-prepare -f examples/data-prepare-conf.yml # the supplied example config
localc
is LibreOffice's spreadsheet program.