我正在使用Kdb+库将数据从DataFrame中的键表导入到熊猫DataFrame。如果我运行同步查询
    x=q.sync('select from prod where ID=9 ') 则x为qpython.qcollection.QKeyedTable型。但如果我制作numpy_temporals=true,返回类型是熊猫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.valuesx.iloc0:1,0:1返回
EMP_ID   PROD_ID   month   total   x 
01        02       jan-17    5.5   6x.columns.values返回
['month' 'total' 'x']数据来自键控表,DataFrame无法访问主键字段。该表有5个字段,但返回的数据帧仅显示3个。我无法访问前两列。
我看过以下堆栈溢出问题-- 无法查看Pandas数据帧中的所有列,Python熊猫,如何扩大输出显示,以看到更多的列?,但它们不能解决问题。
另外,我希望将DataFrame中的数据读入类Employee中,以便为每个员工创建一个特征向量。我不希望数据存储在DataFrame中,因为某些功能将像organization一样具有多值性(员工可能在多个组织中兼职工作)。
我做得对吗?还是有更好的方法来解决这个问题?
发布于 2017-07-20 08:11:53
你看的是一个键控表--将键转换成熊猫DataFrame,使键成为表的索引-
Q过程
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索引除外),请将查询修改为
> x = q.sync('0!select from t', pandas=True) 注意由0!执行的解键操作。
> x.columns.values
array(['a', 'b', 'c', 'd'], dtype=object)值得一读的是qpython文档,因为它确实涵盖了这一点。
https://stackoverflow.com/questions/45207225
复制相似问题