2015-12-02 131 views
1

我得到的列名的列表如下:如何使用列名稱從系列獲取值?

featuresA = [str(col) + '_x' for col in group.index] 

其中組系列。我收到的像「Col1_x」 10列名的列表,「Col2_x」等

現在我想讀系列值到一個數據幀對象:

mergedrow = pd.DataFrame() 
mergedrow[featuresA] = group[featuresA] 

錯誤消息說:

raise KeyError('%s not in index' % objarr[mask]) 

當我使用group.to_frame(),則結果爲0。

的完整代碼如下所示直接轉換到group數據幀:

featuresA = [str(col) + '_x' for col in group.index] 
featuresB = [str(col) + '_y' for col in match.iloc[[idx]].columns] 
mergedrow = pd.DataFrame() 
mergedrow[featuresA] = group[featuresA] 
mergedrow[featuresB] = match.iloc[[idx]] 

UPDATE: 這是整個錯誤消息:

raise KeyError('%s not in index' % objarr[mask]) 
KeyError: "['airportResources.baggage_x' 'airportResources.arrivalTerminal_x'\n 'arrivalAirportFsCode_x' 'operationalTimes.scheduledGateArrival.dateLocal_x'\n 'schedule.flightType_x' 'schedule.serviceClasses_x' 'status_x'\n 'operationalTimes.actualDateTime_x'] not in index" 
+0

你可以張貼2線組的? 'mergedrow [featuresA] = group [featuresA]'實際上你的數據框還沒有被創建,所以當你使用'mergedrow [featuresA]'時會引發'鍵錯誤。 – WoodChopper

回答

0

Series.values給出的數值爲您打造數據幀。

如果你正在嘗試一系列轉換成一個數據框,你可以做到這樣的,

import pandas as pd 

In [33]: group 
Out[33]: 
a 0.316286 
b -0.338733 
c -1.050260 
d -0.365461 
e 0.996902 
dtype: float64 

In [34]: group.index 
Out[34]: Index([u'a', u'b', u'c', u'd', u'e'], dtype='object') 

In [35]: group.values 
Out[35]: array([ 0.31628576, -0.33873327, -1.05026027, -0.3654615 , 0.99690196]) 

In [38]: featuresA = [str(col) + '_x' for col in group.index] 

In [39]: df = pd.DataFrame([group.values], columns = featuresA) 

In [40]: df 
Out[40]: 
    a_x  b_x  c_x  d_x  e_x 
0 0.316286 -0.338733 -1.05026 -0.365461 0.996902