-
Notifications
You must be signed in to change notification settings - Fork 11
Add columns filter
The Add columns HXL filter expands a dataset with new columns, adding the same fixed value in every row.
For example, if you collect a 500-row dataset where every row applies to Kenya, you probably won't add a #country
column and type "Kenya" in 500 times under it; however, when you exchange that dataset with others, they may need that information, since they may be working with datasets from several countries. Other fixed values — like the date or the reporting organisation — are also good candidates for this filter.
The filter takes one or more new-columns specifications. The specification can be as simple as a regular HXL tag spec (with or without the leading #
) followed by =
and a value, like #org+reporting=CRS
or date=2015-10-01
. You can also include a new text header by placing it before the #
; for example, this specification adds the tag #country
and the text header "Country name": Country name#country=Kenya
TODO: row formulas
The filter also allows you to specify whether new columns should appear after existing columns (the default) or before them.
Goal: add the date "2015-03-31" to the beginning of every row in a dataset. Use the new-column specification date=2015-03-31
and set the flag to place the column before existing ones.
Original dataset:
#org | #sector | #adm1 |
---|---|---|
Org A | WASH | Coast District |
Org B | Health | Mountain District |
Org C | Education | Plains District |
Org D | WASH | Coast District |
Filtered dataset:
#date | #org | #sector | #adm1 |
---|---|---|---|
2015-03-31 | Org A | WASH | Coast District |
2015-03-31 | Org B | Health | Mountain District |
2015-03-31 | Org C | Education | Plains District |
2015-03-31 | Org D | WASH | Coast District |
On the command line, use the hxladd program (hxladd -h
for help):
hxladd -b -s 'date=2015-03-31'
In a Python script, use the add_columns method:
hxl.data(url).add_columns('date=2015-03-31', before=True)
You can also include a list of new-column specifications:
hxl.data(url).add_columns(['date=2015-03-31', 'country=Kenya'], before=True)
To create an add columns filter in a JSON spec, use the following properties:
Property | Required? | Value |
---|---|---|
filter | yes | always "add_columns" |
specs | yes | list of new-column specs (see above) |
before | no | boolean |
Example:
{
"filter": "add_columns",
"specs": ["#country+name=Uganda", "#country+code=UGA"],
"before": true
}
Standard: http://hxlstandard.org | Mailing list: [email protected]