Qu

ncol

Builtin index · Collections & strings

result = ncol(df)

Input: df · Output: result

What it is

df is a Table.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = ncol(df)
df Tablenot documented yet
result number
Returns a number: the column count. df.ncols is property-style sugar for this — see below.

Examples

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

df = table(x=[1,2,3], y=[10,20,-5])
print(df)
print("nrow = {nrow(df)}")
print("ncol = {ncol(df)}")
print("df.nrows = {df.nrows}")
print("rows(df) = {rows(df)}")
output
x   y
-  --
1  10
2  20
3  -5
nrow = 3
ncol = 2
df.nrows = 3
rows(df) = 3

from tour

df = DataFrame(x=[1, 2, 3, 4, 5], y=[10, 20, 30, 40, 50], label="sample")
print(nrow(df))            # 5

print(ncol(df))             # 3

tall = filter(df, df.x > 2)
xy = select(tall, "x", "y")
print(xy)
print(describe(df))
output
5
3
x   y
-  --
3  30
4  40
5  50
stat          x          y
-----  --------  ---------
count         5          5
mean          3         30
std    1.581139  15.811388
min           1         10
25%           2         20
    ... 3 more lines

from collections-strings

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

How it works

nrow/ncol answer for anything shape answers for, and they answer it by ASKING shape -- nrow(x) is shape(x)[0] by construction, so the two cannot drift. They used to take a table and nothing else, which made nrow(m) on a matrix -- the obvious spelling -- an error naming tables. The duplication was already flagged in shape_of's own comment, which notes that rows/cols "must return exactly the same values the dedicated nrow/ncol match arms compute": two places computing one answer, kept in step by hand. shape_of returns None for a value with no shape rather than the old silent (0, 0), so these refuse instead of reporting zero rows for a function.

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.