2017-10-08 16 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)