2016-11-18 55 views
1

我有一個數據幀,其中有列'flow1,flow2,flow3' 我需要提取我可以從列中獲得的不同順序。提取數據幀中列名的組合

df1 = pd.read_csv("Desktop/lab9/cleaning1/1160/flow.tsv", sep = "\t", header = None, na_values=[" "]) 
df1.columns = ['flow1', 'flow2', 'flow3'] 
for p in permutations(df1.columns, 3): 
    print p 

這是給我

('flow1', 'flow2', 'flow3') 
('flow1', 'flow3', 'flow2') 
('flow2', 'flow1', 'flow3') 
('flow2', 'flow3', 'flow1') 
('flow3', 'flow1', 'flow2') 
('flow3', 'flow2', 'flow1') 

但我只需要:

('flow1', 'flow2', 'flow3') 
('flow1', 'flow3', 'flow2') 
('flow2', 'flow3', 'flow1') 
+0

任何理由你不需要別人?從3種替代品中進行選擇時,總的獨特排列是3! = 6,正如函數'permutations'給出的那樣。你爲什麼只想要那三個? – Kartik

回答

0

您誤將數學運算符。

使用from itertools import combinations代替permutations

+0

您可以包括: - [t + tuple(set(df1.columns)-set(t))for t in combination(df1.columns,2)]'精確。 –