TidyData: Writing To Csv#
Here we give an example of creating TidyData and writing it to a tidy csv.
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 |
Example#
The simplest way to write tidydata to csv is via the .to_csv()
method of the TidyData
class.
The key point is the last line and the to_csv()
method.
from tidychef import acquire, preview, filters
from tidychef.direction import right, below
from tidychef.output import TidyData, Column
from tidychef.selection import CsvSelectable
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("Observations")
bands = (table.excel_ref("A3") | table.excel_ref("G3")).label_as("Bands")
assets = table.excel_ref('2').is_not_blank().label_as("Assets")
members = (table.excel_ref("B") | table.excel_ref("H")).is_not_blank().label_as("Members")
preview(observations, bands, assets, members)
tidy_data = TidyData(
observations,
Column(bands.attach_closest(right)),
Column(assets.attach_directly(below)),
Column(members.attach_directly(right))
)
print(tidy_data)
tidy_data.to_csv("output.csv")
Observations |
Bands |
Assets |
Members |
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 |
Observations | Bands | Assets | Members |
1 | Beatles | Houses | John |
5 | Beatles | Cars | John |
9 | Beatles | Boats | John |
2 | Rolling Stones | Houses | Keith |
6 | Rolling Stones | Cars | Keith |
10 | Rolling Stones | Boats | Keith |
2 | Beatles | Houses | Paul |
6 | Beatles | Cars | Paul |
10 | Beatles | Boats | Paul |
3 | Rolling Stones | Houses | Mick |
7 | Rolling Stones | Cars | Mick |
11 | Rolling Stones | Boats | Mick |
2 | Beatles | Houses | George |
7 | Beatles | Cars | George |
11 | Beatles | Boats | George |
3 | Rolling Stones | Houses | Charlie |
8 | Rolling Stones | Cars | Charlie |
12 | Rolling Stones | Boats | Charlie |
4 | Beatles | Houses | Ringo |
8 | Beatles | Cars | Ringo |
12 | Beatles | Boats | Ringo |
5 | Rolling Stones | Houses | Ronnie |
9 | Rolling Stones | Cars | Ronnie |
13 | Rolling Stones | Boats | Ronnie |
The “output.csv” data we’ve just extracted can be downloaded here but for convenience purposes we’ll print the contents below.
with open("./output.csv") as f:
print(f.read())
Observations,Bands,Assets,Members
1,Beatles,Houses,John
5,Beatles,Cars,John
9,Beatles,Boats,John
2,Rolling Stones,Houses,Keith
6,Rolling Stones,Cars,Keith
10,Rolling Stones,Boats,Keith
2,Beatles,Houses,Paul
6,Beatles,Cars,Paul
10,Beatles,Boats,Paul
3,Rolling Stones,Houses,Mick
7,Rolling Stones,Cars,Mick
11,Rolling Stones,Boats,Mick
2,Beatles,Houses,George
7,Beatles,Cars,George
11,Beatles,Boats,George
3,Rolling Stones,Houses,Charlie
8,Rolling Stones,Cars,Charlie
12,Rolling Stones,Boats,Charlie
4,Beatles,Houses,Ringo
8,Beatles,Cars,Ringo
12,Beatles,Boats,Ringo
5,Rolling Stones,Houses,Ronnie
9,Rolling Stones,Cars,Ronnie
13,Rolling Stones,Boats,Ronnie
Custom Csv Writing#
The .to_csv()
method uses the csv.writer()
method from the standard python library as documented here. It also propagates keyword arguments to the csv.writer()
method.
Example: using a different delimiter
tidy_data.to_csv("output2.csv", delimiter="|")
# Now print the contents of output2
with open("./output2.csv") as f:
print(f.read())
Observations|Bands|Assets|Members
1|Beatles|Houses|John
5|Beatles|Cars|John
9|Beatles|Boats|John
2|Rolling Stones|Houses|Keith
6|Rolling Stones|Cars|Keith
10|Rolling Stones|Boats|Keith
2|Beatles|Houses|Paul
6|Beatles|Cars|Paul
10|Beatles|Boats|Paul
3|Rolling Stones|Houses|Mick
7|Rolling Stones|Cars|Mick
11|Rolling Stones|Boats|Mick
2|Beatles|Houses|George
7|Beatles|Cars|George
11|Beatles|Boats|George
3|Rolling Stones|Houses|Charlie
8|Rolling Stones|Cars|Charlie
12|Rolling Stones|Boats|Charlie
4|Beatles|Houses|Ringo
8|Beatles|Cars|Ringo
12|Beatles|Boats|Ringo
5|Rolling Stones|Houses|Ronnie
9|Rolling Stones|Cars|Ronnie
13|Rolling Stones|Boats|Ronnie