https://learninghub.kx.com/forums/topic/qpython-to-select-columns-from-table
This is about qpython code. Let’s say we have a table named “t” with 2 columns “name” and “iq” in kdb:
from qpython import qconnection, qcollection
q = qconnection.QConnection(host='localhost', port=5000): q.open()
q("t:flip `name`iq!(`Dent`Beeblebrox`Prefect;98 42 126)")
How can we pass the column name and table name through qpython? Thanks.
pseudo code:
q({select x from y}, "iq", "t")
Some ways:
q("{((),x)#y}", numpy.string_("iq"), numpy.string_("t"))
q("{value"select ",string[x],"from ",string y}", "iq", "t")
q("select %s from %s" % ("iq","t"))
q("{?[y;();0b;{x!x}(),x]}", numpy.string_("iq"), numpy.string_("t"))
- Using # https://code.kx.com/q/ref/take/
- Using value https://code.kx.com/q/ref/value/
- Python string formatting
- Functional select https://code.kx.com/q/basics/funsql/
Functional selects are the most powerful way
https://code.kx.com/q/wp/parse-trees/
Thanks @rocuinneagain. I was trying the functional select and have to convert the strings to <class ‘numpy.bytes_’> first (I am using python 3), i.e.
q("{?[y;();0b;{x!x}(),x]}", np.bytes_("iq"), np.bytes_("t"))
See docs for more on how the library handles difference between symbols and strings:
https://qpython.readthedocs.io/en/latest/type-conversion.html#string-and-symbols
(Answer above updated to use numpy.string_
to pass symbols as needed)