![](/img/trans.png)
[英]pandas DataFrame drops index when passing to kdb+ (using qPython API)
[英]Unable to access all the columns in pandas DataFrame where data is imported from kdb+ using qPython
我正在使用qPython庫將Kdb +中的鍵控表中的數據導入到pandas DataFrame中。 如果我運行同步查詢
x=q.sync('select from prod where ID=9 ')
那么x是qpython.qcollection.QKeyedTable
類型。 但是,如果我使numpy_temporals=true
則返回類型為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]返回
EMP_ID PROD_ID month total x
01 02 jan-17 5.5 6
x.columns.values返回
['month' 'total' 'x']
數據來自鍵控表,DataFrame無法訪問主鍵字段。 該表有5個字段,但返回的數據框僅顯示3個。我無法訪問前兩列。
我看過以下stackoverflow問題無法查看Pandas Data frame和Python pandas中的 所有列 ,如何擴大輸出顯示以查看更多列? 但他們不能解決問題。
另外,我想從DataFrame中將數據讀取到Employee
類中,以便為每個雇員創建一個特征向量。 我不希望將數據存儲在DataFrame中,因為某些功能將像organization
一樣被多值化(員工可能在多個組織中兼職工作)。
我是做對了還是有更好的方法來解決這個問題。
您正在查看鍵控表-轉換為pandas 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!
執行的解鎖0!
。
> x.columns.values
array(['a', 'b', 'c', 'd'], dtype=object)
值得閱讀qpython文檔 ,因為它確實涵蓋了這一點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.