TidyData: Constants#
This section details how to do that.
Source Data#
The data source we’re using for these examples is shown below:
The full data source can be downloaded here.
from tidychef import acquire, preview
from tidychef.selection import CsvSelectable
# Argument is any csv file accessible via http or https
table: CsvSelectable = acquire.csv.http("https://raw.githubusercontent.com/mikeAdamss/tidychef/main/tests/fixtures/csv/bands-wide.csv")
preview(table)
Unnamed Table
A | B | C | D | E | F | G | H | I | J | K | |
1 | |||||||||||
2 | Houses | Cars | Boats | Houses | Cars | Boats | |||||
3 | Beatles | Rolling Stones | |||||||||
4 | John | 1 | 5 | 9 | Keith | 2 | 6 | 10 | |||
5 | Paul | 2 | 6 | 10 | Mick | 3 | 7 | 11 | |||
6 | George | 2 | 7 | 11 | Charlie | 3 | 8 | 12 | |||
7 | Ringo | 4 | 8 | 12 | Ronnie | 5 | 9 | 13 | |||
8 |
Declaring A Constant#
Delaring a constant value columns is a simple process and uses the syntax:
Column.constant(<column name>, <constant value>)
An example follows where we declare two columns of constants to go alongside a single directional column relationship.
from tidychef import acquire, preview, filters
from tidychef.direction import below
from tidychef.selection import CsvSelectable
from tidychef.output import TidyData, Column
# Argument is any csv file accessible via http or https
table: CsvSelectable = acquire.csv.http("https://raw.githubusercontent.com/mikeAdamss/tidychef/main/tests/fixtures/csv/bands-wide.csv")
observations = table.filter(filters.is_numeric).label_as("Value")
assets = table.excel_ref('2').is_not_blank().label_as("Assets Via Direction")
preview(observations, assets)
tidy_data = TidyData(
observations,
Column(assets.attach_directly(below)),
Column.constant("Constant Foo", "Foo"),
Column.constant("Constant Bar", "Bar")
)
print(tidy_data)
Value |
Assets Via Direction |
Unnamed Table
A | B | C | D | E | F | G | H | I | J | K | |
1 | |||||||||||
2 | Houses | Cars | Boats | Houses | Cars | Boats | |||||
3 | Beatles | Rolling Stones | |||||||||
4 | John | 1 | 5 | 9 | Keith | 2 | 6 | 10 | |||
5 | Paul | 2 | 6 | 10 | Mick | 3 | 7 | 11 | |||
6 | George | 2 | 7 | 11 | Charlie | 3 | 8 | 12 | |||
7 | Ringo | 4 | 8 | 12 | Ronnie | 5 | 9 | 13 | |||
8 |
Value | Assets Via Direction | Constant Foo | Constant Bar |
1 | Houses | Foo | Bar |
5 | Cars | Foo | Bar |
9 | Boats | Foo | Bar |
2 | Houses | Foo | Bar |
6 | Cars | Foo | Bar |
10 | Boats | Foo | Bar |
2 | Houses | Foo | Bar |
6 | Cars | Foo | Bar |
10 | Boats | Foo | Bar |
3 | Houses | Foo | Bar |
7 | Cars | Foo | Bar |
11 | Boats | Foo | Bar |
2 | Houses | Foo | Bar |
7 | Cars | Foo | Bar |
11 | Boats | Foo | Bar |
3 | Houses | Foo | Bar |
8 | Cars | Foo | Bar |
12 | Boats | Foo | Bar |
4 | Houses | Foo | Bar |
8 | Cars | Foo | Bar |
12 | Boats | Foo | Bar |
5 | Houses | Foo | Bar |
9 | Cars | Foo | Bar |
13 | Boats | Foo | Bar |