简体   繁体   English

如何解决这个问题? 如何处理我的错误以停止循环

[英]How to solve this ? how to handling my mistake to stop loop

this my data :这是我的数据:

data = {
    'Customer_ID':['CUS_0xd40','CUS_0xd40','CUS_0xd40', 'CUS_0x21b1', 'CUS_0x2dbc'],
    'SNN': ['821-00-0265','#F%$D@*&8','004-07-5839','133-16-7738', '031-35-0942'],
    'Occupation':['Scientist', '_______' ,'Scientist', 'Engineer' ,'Entrepreneur'], 
    'Credit_Mix':['_', 'Good', 'Standard','Bad', 'Standard']  
}

df=pd.DataFrame(data)

I wrote this code but the code has infinite loop how to solve this??我写了这段代码,但代码有无限循环如何解决这个问题? the aim is comparing according to Customer_ID in row( i) androw( i+1) if it the same, this lead to change Credit_Mix and Occupation with same value in next row(i+1)目的是根据第(i)行和第(i+1)行中的Customer_ID进行比较,如果相同,这将导致下一行(i+1)中的Credit_Mix和Occupation具有相同的值

  for i, row in df.iterrows():
        
        while i <=4:
            if df.loc[i, "Customer_ID"] == df.loc[i+1, "Customer_ID"]:
                if row["Credit_Mix"] == '_':
                    df.loc[i, "Credit_Mix"] = df.loc[i+1, "Credit_Mix"]
                else:
                    df.loc[i, "Credit_Mix"] = df.loc[i, "Credit_Mix"]
            
                if row["Occupation"] == '_______':
                    df.loc[i, "Occupation"] = df.loc[i+1, "Occupation"]
                else:
                    df.loc[i, "Occupation"] = df.loc[i, "Occupation"]
            
            else:
                 pass

You are looping over i in the for loop, and in the case i is less than or equal to 4 you are running a while loop.您在 for 循环中循环 i ,并且在 i 小于或等于 4 的情况下,您正在运行一个 while 循环。

for i in range(5):
    print(i)

This code loops over a range of numbers from 0 to 4, the output of the above code will look like this:此代码循环从 0 到 4 的数字范围,上面代码的输出将如下所示:

0
1
2
3
4
for i in range(5):
    print(i)
    while i == 2:
        print("i is 2")

This code does the same, it loops over a range of numbers, from 0 to 4, but when i becomes 2 a while loop starts informing us that i is equal to 2, but i is never incremented so i will stay equal to 2. The output of the above code will look like this:这段代码做同样的事情,它在一个数字范围内循环,从 0 到 4,但是当 i 变为 2 时,while 循环开始通知我们 i 等于 2,但 i 永远不会递增,所以 i 将保持等于 2。上述代码的输出将如下所示:

0
1
2
i is 2
i is 2
i is 2
   .
   .
   .

...and so on, never ending. ...等等,永无止境。

I think your problem is equivalent with this code.我认为您的问题与此代码等效。

def do_something():
    print('do something')

for i, row in enumerate(['a', 'b', 'c', 'd']):
    while i <= 4:
        do_something()

The condition i <= 4 will always be true, try to change your code flow so this condition can be false.条件i <= 4将始终为真,请尝试更改您的代码流,以便此条件可以为假。

q : what i can do in my code to solve this issue?问:我可以在我的代码中做些什么来解决这个问题?
a : If your objective is the aim is comparing according to Customer_ID in row(i) and row(i+1) if it the same . a :如果您the aim is comparing according to Customer_ID in row(i) and row(i+1) if it the same I think you can do like this.我认为你可以这样做。

def do_something():
    print('do something')

for i in range(len(df) - 1):
    is_Customer_ID_i_equal_to_i_plus_1 = df.iloc[i]['Customer_ID'] == df.iloc[i + 1]['Customer_ID']
    print('Customer_ID', i, 'equal to', i + 1, is_Customer_ID_i_equal_to_i_plus_1)
    if is_Customer_ID_i_equal_to_i_plus_1:
        do_something()

The output is like this.输出是这样的。

Customer_ID 0 equal to 1 True
do something
Customer_ID 1 equal to 2 True
do something
Customer_ID 2 equal to 3 False
Customer_ID 3 equal to 4 False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM