Skip to content

Indexing and selection

Select rows, columns and values from a table, relabel it and set its index. These methods are available on pykx.Table and pykx.KeyedTable objects.

head

head(n=5)

Return the first n rows, or all but the last -n rows if n is negative.

Examples:

>>> tab = kx.q('([] a:til 10)')
>>> tab.head(3)
pykx.Table(pykx.q('
a
-
0
1
2
'))

tail

tail(n=5)

Return the last n rows, or all but the first -n rows if n is negative.

Examples:

>>> tab = kx.q('([] a:til 10)')
>>> tab.tail(3)
pykx.Table(pykx.q('
a
-
7
8
9
'))

pop

pop(col_name)

Pop a column / columns from a table by name and return it.

Examples:

>>> tab = kx.q('([] a:1 2 3; b:4 5 6)')
>>> tab.pop('b')
pykx.Table(pykx.q('
b
-
4
5
6
'))
>>> tab
pykx.Table(pykx.q('
a
-
1
2
3
'))

get

get(key, default=None)

Get items from table based on key, if key is not found default is returned.

Examples:

>>> tab = kx.q('([] a:1 2 3; b:4 5 6)')
>>> tab.get('a')
pykx.LongVector(pykx.q('1 2 3'))
>>> tab.get(['a', 'b'])
pykx.Table(pykx.q('
a b
---
1 4
2 5
3 6
'))

at property

at()

Access or set a single value by row label and column name.

Examples:

>>> tab = kx.q('([] a:1 2 3; b:4 5 6)')
>>> tab.at[1, 'b']
pykx.LongAtom(pykx.q('5'))
>>> tab.at[1, 'b'] = 50
>>> tab
pykx.Table(pykx.q('
a b
----
1 4
2 50
3 6
'))

loc property

loc()

Access or set rows and columns by label or by a boolean mask.

Examples:

>>> tab = kx.q('([] a:1 2 3; b:4 5 6)')
>>> tab.loc[tab['a'] > 1, 'b']
pykx.Table(pykx.q('
b
-
5
6
'))

iloc property

iloc()

Access or set rows and columns by integer position.

Examples:

>>> tab = kx.q('([] a:1 2 3; b:4 5 6)')
>>> tab.iloc[[0, 2], ['a']]
pykx.Table(pykx.q('
a
-
1
3
'))

drop

drop(labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors='raise')

Remove rows or columns from a table.

Parameters:

Name Type Description Default
labels str, int or list

The row or column labels to remove, depending on axis.

None
axis int

0 removes rows and 1 removes columns.

0
index list

The row labels to remove, as an alternative to labels with axis=0.

None
columns str or list

The column labels to remove, as an alternative to labels with axis=1.

None
level int or str

The key level to remove rows from, for a keyed table with several key columns.

None
inplace bool

Not implemented, must be False.

False
errors str

'raise' raises an error if a label is not found and 'ignore' skips it.

'raise'

Returns:

Type Description
Table

The table with the rows or columns removed.

Examples:

>>> tab = kx.q('([] a:1 2 3; b:4 5 6)')
>>> tab.drop(columns=['b'])
pykx.Table(pykx.q('
a
-
1
2
3
'))
>>> tab.drop([1])
pykx.Table(pykx.q('
a b
---
1 4
3 6
'))

drop_duplicates

drop_duplicates(subset=None, keep='first', inplace=False, ignore_index=False)

Remove duplicate rows from a table.

Parameters:

Name Type Description Default
subset None

Not yet implemented, must be None.

None
keep str

Only 'first' is implemented.

'first'
inplace bool

Not yet implemented, must be False.

False
ignore_index bool

Not yet implemented, must be False.

False

Returns:

Type Description
Table

The table with only the first of each set of duplicate rows. A KeyedTable is not supported.

Examples:

>>> tab = kx.q('([] a:1 1 2; b:3 3 4)')
>>> tab.drop_duplicates()
pykx.Table(pykx.q('
a b
---
1 3
2 4
'))

rename

rename(
    mapper=None,
    index=None,
    columns=None,
    axis=0,
    copy=None,
    inplace=False,
    level=None,
    errors="ignore",
)

Rename the columns or the index of a table.

Parameters:

Name Type Description Default
mapper dict

A dictionary of names to new names, applied to the axis given by axis.

None
index dict

A dictionary of index names to new names, for a keyed table with a single key column.

None
columns dict

A dictionary of column names to new names.

None
axis int or str

0 or 'index' renames the index and 1 or 'columns' renames the columns.

0
copy bool

Not yet implemented, must be None.

None
inplace bool

Not yet implemented, must be False.

False
level None

Not yet implemented, must be None.

None
errors str

Not yet implemented, must be 'ignore'.

'ignore'

Returns:

Type Description
Table or KeyedTable

The table with the names changed.

Examples:

>>> tab = kx.q('([] a:1 2 3; b:4 5 6)')
>>> tab.rename(columns={'a': 'x'})
pykx.Table(pykx.q('
x b
---
1 4
2 5
3 6
'))

add_suffix

add_suffix(suffix, axis=0)

Add a suffix to the name of each column.

Parameters:

Name Type Description Default
suffix str

The string added to the end of each column name.

required
axis int

0 for a Table and 1 for the value columns of a Table or KeyedTable.

0

Returns:

Type Description
Table

The table with the columns renamed.

Examples:

>>> tab = kx.q('([] a:1 2 3; b:4 5 6)')
>>> tab.add_suffix('_x')
pykx.Table(pykx.q('
a_x b_x
-------
1   4
2   5
3   6
'))

add_prefix

add_prefix(prefix, axis=0)

Add a prefix to the name of each column.

Parameters:

Name Type Description Default
prefix str

The string added to the start of each column name.

required
axis int

0 for a Table and 1 for the value columns of a Table or KeyedTable.

0

Returns:

Type Description
Table

The table with the columns renamed.

Examples:

>>> tab = kx.q('([] a:1 2 3; b:4 5 6)')
>>> tab.add_prefix('x_')
pykx.Table(pykx.q('
x_a x_b
-------
1   4
2   5
3   6
'))

sample

sample(
    n=None, frac=None, replace=False, weights=None, random_state=None, axis=None, ignore_index=False
)

Select random rows from the table.

Parameters:

Name Type Description Default
n int

The number of rows to return. This cannot be used with frac and defaults to 1.

None
frac float

The fraction of the rows to return. This cannot be used with n.

None
replace bool

Whether the same row can be selected more than once.

False
weights None

Not yet implemented, must be None.

None
random_state None

Not yet implemented, must be None.

None
axis int

Not yet implemented, must be None.

None
ignore_index bool

Not yet implemented, must be False.

False

Returns:

Type Description
Table

A table of the selected rows.

Examples:

>>> tab = kx.q('([] a:1 2 3; b:4 5 6)')
>>> tab.sample(2)
pykx.Table(pykx.q('
a b
---
1 4
2 5
'))

set_index

set_index(keys, drop=True, append=False, inplace=False, verify_integrity=False)

Set the index using existing columns.

Examples:

>>> tab = kx.q('([] k:`a`b`c; v:1 2 3)')
>>> tab.set_index('k')
pykx.KeyedTable(pykx.q('
k| v
-| -
a| 1
b| 2
c| 3
'))

index property

index()

The index of the table, which is the key of a keyed table or the row numbers of a table.

Examples:

>>> tab = kx.q('([] k:`a`b`c; v:1 2 3)')
>>> tab.index
pykx.LongVector(pykx.q('0 1 2'))
>>> tab.set_index('k').index
pykx.Table(pykx.q('
k
-
a
b
c
'))

reset_index

reset_index(
    levels=None,
    *,
    drop=False,
    inplace=False,
    col_level=0,
    col_fill="",
    allow_duplicates=False,
    names=None
)

Reset keys/index of a pykx.Table

Examples:

>>> tab = kx.q('1!([] k:`a`b`c; v:1 2 3)')
>>> tab.reset_index()
pykx.Table(pykx.q('
k v
---
a 1
b 2
c 3
'))