将 pandas 数据框保存在kdb/q中 [英] Save pandas dataframe in kdb/q

查看:82
本文介绍了将 pandas 数据框保存在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆