Qu

write_csv

Builtin index · File I/O

write_csv(df, path)

Input: df, path · Output: no output

What it is

Writes a Table to a CSV file. df (Table) is the data to write. path (string) is the destination file.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
write_csv(df, path)
df Tabledf (Table) is the data to write.
path stringpath (string) is the destination file.
no outputWrites a Table to a CSV file.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

write_csv(df, "data.csv")
df2 = read_csv("data.csv")
print(df2)
table.write(df, "data2.csv")
df3 = table.load("data2.csv")
print(df3)
output
x   y
-  --
1  10
2  20
3  -5
x   y
-  --
1  10
2  20
3  -5

from tour

measurements = table(
    x = [-1.5, 0.5, 2.0, -0.25, 3.75],
    sensor = ["a", "b", "a", "c", "b"]
)
write_csv(measurements, "measurements.csv")

df = read_csv("measurements.csv")
print(nrow(df))
print(describe(df))
write_csv(filter(df, df.x > 0), "positive_only.csv")
print(read_all_text("positive_only.csv"))
output
5
stat         x
-----  -------
count        5
mean       0.9
std    2.03562
min       -1.5
25%      -0.25
50%        0.5
75%          2
max       3.75
x,sensor
0.5,b
2,a
    ... 1 more lines

from file-io

write_csv(table(
    temp     = [21.5, 22.1, 35.0, 21.8, 36.2, 22.0, 34.5, 21.6, 35.8, 22.3],
    humidity = [45.2, 46.0, 20.1, 45.5, 19.5, 45.8, 21.0, 45.1, 20.4, 46.2],
    pressure = [1012.1, 1011.8, 1009.5, 1012.0, 1008.9, 1012.3, 1009.8, 1012.2, 1009.1, 1011.9],
    label    = ["ok", "ok", "warn", "ok", "warn", "ok", "warn", "ok", "warn", "ok"]
), "sensors.csv")

df = read_csv("sensors.csv")
print("rows = {nrow(df)}, cols = {ncol(df)}")
print(head(df, 3))

numeric = drop(df, "label")
c = corrmat(numeric)
print(c.matrix)
c.plot()
savefig("quickstart_corrmat.svg")

scaled_temp = standardize(df.temp)
print("standardized temp[0] = {scaled_temp[0]:.4f}")
print("mean(standardized) = {mean(scaled_temp):.4f}, std(standardized) = {std(scaled_temp):.4f}")
output
rows = 10, cols = 4
temp  humidity  pressure  label
----  --------  --------  -----
21.5      45.2    1012.1  ok   
22.1        46    1011.8  ok   
  35      20.1    1009.5  warn 
[1, -0.997357, -0.991317; -0.997357, 1, 0.983511; -0.991317, 0.983511, 1]
· savefig quickstart_corrmat.svg
  note: fonts render using whatever is installed on the viewer's machine (none are embedded) -- pass embed_fonts=true (fontfamily preset only) or export .tikz for guaranteed appearance
standardized temp[0] = -0.8275
mean(standardized) = -0.0000, std(standardized) = 1.0000
temp, temp: 1 1 temp, humidity: -0.997 -0.997 temp, pressure: -0.991 -0.991 humidity, temp: -0.997 -0.997 humidity, humidity: 1 1 humidity, pressure: 0.984 0.984 pressure, temp: -0.991 -0.991 pressure, humidity: 0.984 0.984 pressure, pressure: 1 1 temp humidity pressure temp humidity pressure First plot

from quickstart