在 kdb/q 中保存 pandas 数据框
Save pandas dataframe in kdb/q
在 kdb 中保存 pandas 数据帧的最佳方法是什么?是否有任何库可以使它更容易?
下面的代码显然可以用来从 kdb 中加载一些东西,但是我如何将数据帧保存到其中?
from qpython import qconnection
with qconnection.QConnection(host = 'localhost', port = 5001, pandas = True) as q:
ds = q('(1i;0Ni;3i)', pandas = True)
print(ds)
使用来自 Pandas Integration
的模拟 table
with qconnection.QConnection(host = 'localhost', port = 5000, pandas = True) as q:
df = q('flip `name`iq`fullname!(`Dent`Beeblebrox`Prefect;98 42 126;("Arthur Dent";"Zaphod Beeblebrox"; "Ford Prefect"))')
这会将 table 内置的 kdb 作为 pandas 数据帧拉回到 python 中。以下方法将 table 保存在内存中:
with qconnection.QConnection(host = 'localhost', port = 5000, pandas = True) as q:
q('{`t set x}', df)
可以找到有关如何在 kdb 中保存数据的更多信息:kdb overview
可以在此处找到更多使用 set 保存数据的示例:set
还有一种整合python和q的方法可能对你有用; PyQ 将 Python 和 q 解释器置于同一个进程中,因此用任何一种语言编写的代码都对相同的数据进行操作。
要使用 qPython 在 KDB+ 中保存数据帧,可以使用 QConnection
class 的 sync
方法。将第一个参数设置为定义 q 函数的字符串,该函数将其参数分配给全局变量并将数据帧作为第二个参数发送。像这样:
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
而不是局部变量
一种选择是使用 embedPy,它允许 kdb+ 和 Python 共享相同的进程和内存 space
您可以在提供的 link http://code.kx.com/q/ml/embedpy/
中找到有关此库的文档
参见下面在 kdb+
中实现的示例
q)/ Create kdb+ table
q)n:100;show 5#qtab:([]scol:n?`AAA`BBB`CCC;icol:n?100;fcol:n?1f)
scol icol vcol
-------------------
CCC 59 0.3927524
AAA 30 0.5170911
CCC 85 0.5159796
AAA 89 0.4066642
AAA 23 0.1780839
q)
q)/ Load embedPy and import pandas/DataFrame
q)\l p.q
q)df:(pd:.p.import`pandas)`:DataFrame
q)
q)/ Initialize DataFrame object
q)/ print is built in embedpy
q)print df[qtab][`:head]5
fcol icol scol
0 0.392752 59 CCC
1 0.517091 30 AAA
2 0.515980 85 CCC
3 0.406664 89 AAA
4 0.178084 23 AAA
q)/ need to reorder the columns
q)print ptab:df[qtab][@;cols qtab]
scol icol fcol
0 CCC 59 0.392752
1 AAA 30 0.517091
2 CCC 85 0.515980
3 AAA 89 0.406664
4 AAA 23 0.178084
q)/ and converting back to kdb+
q)/ need to convert it to a dict like
q)5#flip ptab[`:to_dict;`list]`
scol icol fcol
--------------------
"CCC" 59 0.3927524
"AAA" 30 0.5170911
"CCC" 85 0.5159796
"AAA" 89 0.4066642
"AAA" 23 0.1780839
q)/ can also use but slower
q)5#ptab[`:to_dict;`records]`
scol icol fcol
--------------------
"CCC" 59 0.3927524
"AAA" 30 0.5170911
"CCC" 85 0.5159796
"AAA" 89 0.4066642
"AAA" 23 0.1780839
q)/ Consider keyed table
q)show qktab:select by scol from qtab
scol| icol fcol
----| ---------------
AAA | 35 0.3410485
BBB | 61 0.5548864
CCC | 0 0.07347808
q)/ print as dataframe format
q)/ reordering columns to be the same as qktab
q)print pktab:df[qktab][@;cols qktab]
scol icol fcol
0 AAA 35 0.341049
1 BBB 61 0.554886
2 CCC 0 0.073478
q) / convert it to a dataframe keyed table
q) / setting the index to become the keyed column
q)
q)print pktab:pktab[`:set_index]keys qktab
icol fcol
scol
AAA 35 0.341049
BBB 61 0.554886
CCC 0 0.073478
q) / converting back to kdb+
q) / need to `reset_index` to return full table
q) / then key the table
q)(`$pktab[`:index.names]`)xkey flip pktab[`:reset_index][][`:to_dict;`list]`
scol | icol fcol
-----| ---------------
"AAA"| 35 0.3410485
"BBB"| 61 0.5548864
"CCC"| 0 0.07347808
在 kdb 中保存 pandas 数据帧的最佳方法是什么?是否有任何库可以使它更容易?
下面的代码显然可以用来从 kdb 中加载一些东西,但是我如何将数据帧保存到其中?
from qpython import qconnection
with qconnection.QConnection(host = 'localhost', port = 5001, pandas = True) as q:
ds = q('(1i;0Ni;3i)', pandas = True)
print(ds)
使用来自 Pandas Integration
的模拟 tablewith qconnection.QConnection(host = 'localhost', port = 5000, pandas = True) as q:
df = q('flip `name`iq`fullname!(`Dent`Beeblebrox`Prefect;98 42 126;("Arthur Dent";"Zaphod Beeblebrox"; "Ford Prefect"))')
这会将 table 内置的 kdb 作为 pandas 数据帧拉回到 python 中。以下方法将 table 保存在内存中:
with qconnection.QConnection(host = 'localhost', port = 5000, pandas = True) as q:
q('{`t set x}', df)
可以找到有关如何在 kdb 中保存数据的更多信息:kdb overview
可以在此处找到更多使用 set 保存数据的示例:set
还有一种整合python和q的方法可能对你有用; PyQ 将 Python 和 q 解释器置于同一个进程中,因此用任何一种语言编写的代码都对相同的数据进行操作。
要使用 qPython 在 KDB+ 中保存数据帧,可以使用 QConnection
class 的 sync
方法。将第一个参数设置为定义 q 函数的字符串,该函数将其参数分配给全局变量并将数据帧作为第二个参数发送。像这样:
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
而不是局部变量
一种选择是使用 embedPy,它允许 kdb+ 和 Python 共享相同的进程和内存 space
您可以在提供的 link http://code.kx.com/q/ml/embedpy/
中找到有关此库的文档参见下面在 kdb+
中实现的示例 q)/ Create kdb+ table
q)n:100;show 5#qtab:([]scol:n?`AAA`BBB`CCC;icol:n?100;fcol:n?1f)
scol icol vcol
-------------------
CCC 59 0.3927524
AAA 30 0.5170911
CCC 85 0.5159796
AAA 89 0.4066642
AAA 23 0.1780839
q)
q)/ Load embedPy and import pandas/DataFrame
q)\l p.q
q)df:(pd:.p.import`pandas)`:DataFrame
q)
q)/ Initialize DataFrame object
q)/ print is built in embedpy
q)print df[qtab][`:head]5
fcol icol scol
0 0.392752 59 CCC
1 0.517091 30 AAA
2 0.515980 85 CCC
3 0.406664 89 AAA
4 0.178084 23 AAA
q)/ need to reorder the columns
q)print ptab:df[qtab][@;cols qtab]
scol icol fcol
0 CCC 59 0.392752
1 AAA 30 0.517091
2 CCC 85 0.515980
3 AAA 89 0.406664
4 AAA 23 0.178084
q)/ and converting back to kdb+
q)/ need to convert it to a dict like
q)5#flip ptab[`:to_dict;`list]`
scol icol fcol
--------------------
"CCC" 59 0.3927524
"AAA" 30 0.5170911
"CCC" 85 0.5159796
"AAA" 89 0.4066642
"AAA" 23 0.1780839
q)/ can also use but slower
q)5#ptab[`:to_dict;`records]`
scol icol fcol
--------------------
"CCC" 59 0.3927524
"AAA" 30 0.5170911
"CCC" 85 0.5159796
"AAA" 89 0.4066642
"AAA" 23 0.1780839
q)/ Consider keyed table
q)show qktab:select by scol from qtab
scol| icol fcol
----| ---------------
AAA | 35 0.3410485
BBB | 61 0.5548864
CCC | 0 0.07347808
q)/ print as dataframe format
q)/ reordering columns to be the same as qktab
q)print pktab:df[qktab][@;cols qktab]
scol icol fcol
0 AAA 35 0.341049
1 BBB 61 0.554886
2 CCC 0 0.073478
q) / convert it to a dataframe keyed table
q) / setting the index to become the keyed column
q)
q)print pktab:pktab[`:set_index]keys qktab
icol fcol
scol
AAA 35 0.341049
BBB 61 0.554886
CCC 0 0.073478
q) / converting back to kdb+
q) / need to `reset_index` to return full table
q) / then key the table
q)(`$pktab[`:index.names]`)xkey flip pktab[`:reset_index][][`:to_dict;`list]`
scol | icol fcol
-----| ---------------
"AAA"| 35 0.3410485
"BBB"| 61 0.5548864
"CCC"| 0 0.07347808