2017-06-05 59 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[]內使用|