Python 为什么我的代码没有通过前两行

Python 为什么我的代码没有通过前两行,python,pandas,preprocessor,Python,Pandas,Preprocessor,我试图从未加权的二部图中删除自循环,但是我的代码只打印前2行(作为单行),循环的每次迭代都打印这2行(作为单行)。我对熊猫很陌生,可能会犯一些循环错误 我试着只对索引和行进行迭代,但最终结果是一样的 代码: for index,row in dataset.iterrows(): x = next(dataset.iterrows()) y=x[1].to_string().split() if ((y[0])!=(y[1])): outfile.writ

我试图从未加权的二部图中删除自循环,但是我的代码只打印前2行(作为单行),循环的每次迭代都打印这2行(作为单行)。我对熊猫很陌生,可能会犯一些循环错误

我试着只对索引和行进行迭代,但最终结果是一样的

代码

for index,row in dataset.iterrows():
    x = next(dataset.iterrows())
    y=x[1].to_string().split()
    if ((y[0])!=(y[1])):
        outfile.write(x[1].to_string() + "\n")
% bip unweighted

1 1

1 2

1 3

1 4
...
% bip unweighted

1 2

1 3

1 4

...
输入

for index,row in dataset.iterrows():
    x = next(dataset.iterrows())
    y=x[1].to_string().split()
    if ((y[0])!=(y[1])):
        outfile.write(x[1].to_string() + "\n")
% bip unweighted

1 1

1 2

1 3

1 4
...
% bip unweighted

1 2

1 3

1 4

...
实际值

% bip unweighted    1 1

% bip unweighted    1 1 

% bip unweighted    1 1 

...
预期的

for index,row in dataset.iterrows():
    x = next(dataset.iterrows())
    y=x[1].to_string().split()
    if ((y[0])!=(y[1])):
        outfile.write(x[1].to_string() + "\n")
% bip unweighted

1 1

1 2

1 3

1 4
...
% bip unweighted

1 2

1 3

1 4

...

删除
next
语句。您已经在迭代行了,不需要这样做。在每次迭代中,下一行存储在
变量中。用它代替
x
@siddhantandon谢谢,这很有效。代码现在运行正常:)删除
next
语句。您已经在迭代行了,不需要这样做。在每次迭代中,下一行存储在
变量中。用它代替
x
@siddhantandon谢谢,这很有效。代码现在运行正常:)