Skip to content

cols, xcol, xcols

Table columns

cols

Column names of a table

cols x    cols[x]

Where x is a

  • table
  • the name of a table as a symbol atom
  • a filesymbol for a splayed table

returns its column names as a symbol vector.

q)\l trade.q
q)cols trade            /value
 `time`sym`price`size
q)cols`trade            /reference
 `time`sym`price`size

xcol

Rename table columns or dictionary keys

x xcol y    xcol[x;y]

Where y is a table or dictionary passed by value, and x is

  • a symbol vector of length no greater than count cols y returns y with its first count x columns (or keys) renamed
  • a dictionary formed from two symbol vectors, returns y with the columns (or keys) in key x renamed as value x
q)t:([]a:3 4 5; b:6 7 8; c:`z`u`i)
q)`d`e xcol t                               / rename first two columns
d e c
-----
3 6 z
4 7 u
5 8 i
q)([a:`A;c:`C]) xcol t                        / rename selected columns
A b C
-----
3 6 z
4 7 u
5 8 i
q)([q:`r]) xcol t              / nonexistent column names in key x signal a length error
'length
  [0]  ([q:`r]) xcol t

Dictionary as right argument

From KDB-X 2026.10.23, y can also be a dictionary, in which case its keys are renamed.

q)([a:`c;b:`d]) xcol ([a:10;b:20])
c| 10
d| 20
q)`x`y xcol ([a:10;b:20])
x| 10
y| 20

Q for Mortals §9.8.1 xcol

xcols

Reorder table columns or dictionary keys

x xcols y    xcols[x;y]

Where

  • y is a simple table or a dictionary passed by value
  • x is a symbol vector of some or all of y’s column names or keys (can also be an atom)

returns y with x as its first column/s or key/s.

q)t:([]a:3 4 5; b:6 7 8; c:`z`u`i)
q)`b xcols t
b a c
-----
6 3 z
7 4 u
8 5 i
q)t:xcols[reverse cols t;t]              / reverse cols and reassign
q)cols t
`c`b`a

Dictionary as right argument

From KDB-X 2026.10.23, y can also be a dictionary, in which case its keys are reordered.

q)`c xcols ([a:1;b:2;c:3])
c| 3
a| 1
b| 2

Q for Mortals §9.8.2 xcols


Dictionaries, Metadata

Tables