2017-10-08 14 views
-1

使用itertools我進口,其中的數據看起來像在python

25 
34 
45 
23 
34 
23 
24 

    import itertools 
    import pandas as pd 
    filename = 'myFile.csv' 
    myArray1 = pd.read_csv(filename, header=None) 

    myArray2 = pd.read_csv(filename, header=None) 


    for a, b in itertools.product(myArray1, myArray2): 
     print(a,b) 

但只輸出

0 0 

當我運行一個CSV文件:

myArray1 = [2,3,4] 
myArray2 = [4,5,6] 
for a, b in itertools.product(myArray1, myArray2): 
    print(a,b) 

輸出就像

2 4 
2 5 
2 6 
3 4 
3 5 
3 6 
4 4 
4 5 
4 6 

所以我想從這樣的CSV數據輸出

+0

如果沒有更多的CSV數據,這將很難診斷。 'itertools.product'之前'myArray1'和'myArray2'的輸出是什麼? –

+0

對不起,但您已編輯您的問題與其他問題。你已經有效地改變了你的問題陳述。如果您有其他問題,請關閉此問題,接受答案,並打開一個新問題。我已經回滾了你的編輯,它會讓讀者感到困惑。請不要再這樣做。 –

回答

4

會發生什麼是默認情況下對數據幀的迭代發生在其列標籤上。在你的情況中,每個數據幀中只有一列標記爲0,所以你最終得到一個輸出(0, 0)

作爲解決方案,您需要提取這些值,然後然後將它傳遞給product。爲了提高效率,只做一次。

data = pd.read_csv(filename, header=None).iloc[:, 0].values.tolist() 
for a, b in itertools.product(data, data): 
    print(a, b)