TidyData: Writing To Csv

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

ABCDEFGHIJK
1
2HousesCarsBoatsHousesCarsBoats
3BeatlesRolling Stones
4John159Keith2610
5Paul2610Mick3711
6George2711Charlie3812
7Ringo4812Ronnie5913
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

ABCDEFGHIJK
1
2HousesCarsBoatsHousesCarsBoats
3BeatlesRolling Stones
4John159Keith2610
5Paul2610Mick3711
6George2711Charlie3812
7Ringo4812Ronnie5913
8

ObservationsBandsAssetsMembers
1BeatlesHousesJohn
5BeatlesCarsJohn
9BeatlesBoatsJohn
2Rolling StonesHousesKeith
6Rolling StonesCarsKeith
10Rolling StonesBoatsKeith
2BeatlesHousesPaul
6BeatlesCarsPaul
10BeatlesBoatsPaul
3Rolling StonesHousesMick
7Rolling StonesCarsMick
11Rolling StonesBoatsMick
2BeatlesHousesGeorge
7BeatlesCarsGeorge
11BeatlesBoatsGeorge
3Rolling StonesHousesCharlie
8Rolling StonesCarsCharlie
12Rolling StonesBoatsCharlie
4BeatlesHousesRingo
8BeatlesCarsRingo
12BeatlesBoatsRingo
5Rolling StonesHousesRonnie
9Rolling StonesCarsRonnie
13Rolling StonesBoatsRonnie


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