2017-06-05 99 views
0

我想根据以前从过滤下面的数据框获得的列过滤数据框。使用Python中其他数据框的标题过滤数据框

AA BB CC DD EE FF GG 
0 1 1 0 1 0 0 

该数据帧是从一个文件来,其中每行中的数据可以是一个0或1,并基于该被加载的文件将会改变。我已经使用以下代码来过滤这个数据帧,以便我的输出只包含值为1的列。

with open('Factors.txt') as b: 
    IncludedFactors = pd.read_table(b, sep=',') 
print IncludedFactors 

InterestingFactors = IncludedFactors.drop(IncludedFactors.columns[~IncludedFactors.iloc[0].astype(bool)],axis=1) 
print InterestingFactors 

output: 
BB CC EE 
1 1 1 

然后我需要过滤掉有许多头一个更大的数据帧,但我只需要ID,x向位置,Yposition和InterestingFactors数据框的标题。

下面是我尝试过的代码,但输出仍然只包含3个头,而不是我需要的6个头。

headers = InterestingFactors.columns.values 
print headers 
PivotTable = InfoTable.filter(items=['ID', 'Postion_X','Position_Y','headers']) 
print PivotTable 

任何关于如何正确地做到这一点的帮助是非常感谢!

回答

1

这里是你可以做到这一点的一种方法:

headers = InterestingFactors.columns.append(pd.Index(['ID','Postion_X','Position_Y'])) 
PivotTable = InfoTable.loc[:, headers] 

这样就结合你正在寻找从InterestingFactors与您在上面提到的3列的列。此Index传递给.loc[]

这也适用于:

headers = InterestingFactors.columns 
PivotTable = InfoTable.loc[:, (pd.Index(['ID','Postion_X','Position_Y']) | headers)] 

比较的数据类型(我相信)必须是相同的。将3列标准列转换为pd.Index将允许您在.loc[]内使用|

相关问题