无法访问 pandas DataFrame 中的所有列,其中使用 qPython 从 kdb+ 导入数据
Unable to access all the columns in pandas DataFrame where data is imported from kdb+ using qPython
我正在使用 qPython 库将数据从 Kdb+ 中的键控 table 导入到 pandas DataFrame。如果我运行一个同步查询
x=q.sync('select from prod where ID=9 ')
则x的类型为qpython.qcollection.QKeyedTable
。但是如果我让 numpy_temporals=true
类型 return 是 pandas DataFrame.
from qpython import qconnection
with qconnection.QConnection(host = 'localhost', port = 5000) as q:
query = 'select from table where ID=5'
x=q.sync(query, numpy_temporals = True)
print x.iloc[0:3,0:3]
print x.columns.values
x.iloc[0:1,0:1] returns
EMP_ID PROD_ID month total x
01 02 jan-17 5.5 6
x.columns.valuesreturns
['month' 'total' 'x']
数据来自键控 table,DataFrame 无法访问主键字段。 table 有 5 个字段,但 returned 数据框只显示 3 个。我无法访问前两列。
我查看了以下 Whosebug 问题 , Python pandas, how to widen output display to see more columns? 但它们没有解决问题。
另外我想把DataFrame中的数据读入classEmployee
,从而为每个员工创建一个特征向量。我不希望将数据存储在 DataFrame 中,因为某些功能将是多值的,例如 organization
(该员工可能在多个组织兼职)。
我这样做是否正确,或者有更好的方法来解决这个问题。
您正在查看键控 table - 转换为 pandas DataFrame 使键成为 table -
的索引
问进程
q)\p 5000
q)t:([a:til 10;b:reverse til 10]c:10?`3;d:10?10i)
Python 进程
> import pandas as pd
> import numpy as np
> from qpython.qconnection import QConnection as qc
> q = qc('localhost', 5000)
> q.open()
> x = q.sync('select from t', pandas=True)
> x.columns.values
array(['c', 'd'], dtype=object)
> x.index
MultiIndex(levels=[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]],
labels=[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]],
names=[u'a', u'b'])
如果您希望将所有列视为标准 DataFrame,没有索引(标准 i-indexing 除外),请将查询修改为
> x = q.sync('0!select from t', pandas=True)
注意 0!
执行的取消加密。
> x.columns.values
array(['a', 'b', 'c', 'd'], dtype=object)
qpython documentation 值得一读,因为它涵盖了这一点。
我正在使用 qPython 库将数据从 Kdb+ 中的键控 table 导入到 pandas DataFrame。如果我运行一个同步查询
x=q.sync('select from prod where ID=9 ')
则x的类型为qpython.qcollection.QKeyedTable
。但是如果我让 numpy_temporals=true
类型 return 是 pandas DataFrame.
from qpython import qconnection
with qconnection.QConnection(host = 'localhost', port = 5000) as q:
query = 'select from table where ID=5'
x=q.sync(query, numpy_temporals = True)
print x.iloc[0:3,0:3]
print x.columns.values
x.iloc[0:1,0:1] returns
EMP_ID PROD_ID month total x
01 02 jan-17 5.5 6
x.columns.valuesreturns
['month' 'total' 'x']
数据来自键控 table,DataFrame 无法访问主键字段。 table 有 5 个字段,但 returned 数据框只显示 3 个。我无法访问前两列。
我查看了以下 Whosebug 问题
另外我想把DataFrame中的数据读入classEmployee
,从而为每个员工创建一个特征向量。我不希望将数据存储在 DataFrame 中,因为某些功能将是多值的,例如 organization
(该员工可能在多个组织兼职)。
我这样做是否正确,或者有更好的方法来解决这个问题。
您正在查看键控 table - 转换为 pandas DataFrame 使键成为 table -
的索引问进程
q)\p 5000
q)t:([a:til 10;b:reverse til 10]c:10?`3;d:10?10i)
Python 进程
> import pandas as pd
> import numpy as np
> from qpython.qconnection import QConnection as qc
> q = qc('localhost', 5000)
> q.open()
> x = q.sync('select from t', pandas=True)
> x.columns.values
array(['c', 'd'], dtype=object)
> x.index
MultiIndex(levels=[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]],
labels=[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]],
names=[u'a', u'b'])
如果您希望将所有列视为标准 DataFrame,没有索引(标准 i-indexing 除外),请将查询修改为
> x = q.sync('0!select from t', pandas=True)
注意 0!
执行的取消加密。
> x.columns.values
array(['a', 'b', 'c', 'd'], dtype=object)
qpython documentation 值得一读,因为它涵盖了这一点。