2015-12-02 135 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