将 pandas 数据框保存在kdb/q中 [英] Save pandas dataframe in kdb/q
问题描述
在kdb中保存熊猫数据框的最佳方法是什么?有没有可以简化使用的库?
What is the best way to save a pandas dataframe in kdb? Are there any libraries that can make it easier?
下面的代码显然可以用于从kdb中删除某些内容,但是如何将数据帧保存到其中呢?
The below code can apparently be used to loas something from kdb, but how do I save a dataframe into it?
from qpython import qconnection
with qconnection.QConnection(host = 'localhost', port = 5001, pandas = True) as q:
ds = q('(1i;0Ni;3i)', pandas = True)
print(ds)
推荐答案
要使用qPython在KDB +中保存数据框,可以使用QConnection
类的sync
方法.将第一个参数设置为定义q函数的字符串,该函数将其参数分配给全局变量并将数据帧作为第二个参数发送.像这样:
To save a dataframe in KDB+ with qPython one can use the sync
method of the QConnection
class. Set the first parameter to a string defining a q function that assigns its parameter to a global variable and send the dataframe as the second parameter. Something like this:
from qpython import qconnection
import pandas as pd
df = pd.DataFrame({'sym':['abc','def','ghi'],'price':[10.1,10.2,10.3]})
with qconnection.QConnection(host = 'localhost', port = 5001, pandas = True) as q:
q.sync('{t::x}',df)
请注意,您需要在函数定义中使用双冒号::
,以便将参数分配给全局变量t
而不是局部变量.
Note that you need to use a double colon ::
in the function definition so that the parameter is assigned to a global variable t
rather that a local variable.
这篇关于将 pandas 数据框保存在kdb/q中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!