Function application¶
Apply a function to the values of a table. These methods are available on pykx.Table and pykx.KeyedTable objects.
apply ¶
apply(func, *args, axis=0, raw=None, result_type=None, **kwargs)
Apply a function along an axis of the table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func |
callable
|
The function to apply to each column or row, which is passed as a
|
required |
*args |
Any
|
Positional arguments passed to |
()
|
axis |
int
|
0 applies the function to each column and 1 to each row. |
0
|
raw |
None
|
Not yet implemented, must be |
None
|
result_type |
None
|
Not yet implemented, must be |
None
|
**kwargs |
Any
|
Keyword arguments passed to |
{}
|
Returns:
| Type | Description |
|---|---|
List, Dictionary or Table
|
The result of |
Examples:
>>> tab = kx.q('([] a:1 2 3; b:4 5 6)')
>>> tab.apply(kx.q.sum)
pykx.Dictionary(pykx.q('
a| 6
b| 15
'))
map ¶
map(func, na_action=None, *args, **kwargs)
Apply a function to every element of the table.
A vectorized operation, such as tab * 2, is much faster than map with a function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func |
callable
|
The function to apply to each element. |
required |
na_action |
str
|
If |
None
|
*args |
Any
|
Positional arguments passed to |
()
|
**kwargs |
Any
|
Keyword arguments passed to |
{}
|
Returns:
| Type | Description |
|---|---|
Table
|
The table of the results. |
Examples:
>>> tab = kx.q('([] a:1 2 3; b:4 5 6)')
>>> tab.map(lambda x: x * 2)
pykx.Table(pykx.q('
a b
----
2 8
4 10
6 12
'))
applymap ¶
applymap(func, na_action=None, *args, **kwargs)
Apply a function to every element of the table. This is an alias of map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func |
callable
|
The function to apply to each element. |
required |
na_action |
str
|
If |
None
|
*args |
Any
|
Positional arguments passed to |
()
|
**kwargs |
Any
|
Keyword arguments passed to |
{}
|
Returns:
| Type | Description |
|---|---|
Table
|
The table of the results. |
Examples:
>>> tab = kx.q('([] a:1 2 3; b:4 5 6)')
>>> tab.applymap(lambda x: x * 2)
pykx.Table(pykx.q('
a b
----
2 8
4 10
6 12
'))