Showing results for 
Search instead for 
Did you mean: 

qpython to select columns from table

New Contributor III

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("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")



Contributor III
Contributor III

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"))



  1. Using #
  2. Using value 
  3. Python string formatting
  4. Functional select 

Functional selects are the most powerful way 

New Contributor III

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: 

(Answer above updated to use numpy.string_ to pass symbols as needed)